Creating instance variables while looping over a hash

Discussion in 'Ruby' started by blaine, Jul 25, 2007.

  1. blaine

    blaine Guest

    I"m reading in a YAML file using the yaml library in Ruby. My YAML
    looks something like this:

    config:
    value1: Something here
    value2: Something else

    As I loop from the YAML like:

    config["config"].each { |key, value| }

    How could I set the key as an instance variable with the value of the
    value of the key... resulting in:

    @value1 = "Something here"
    @value2 = "Something else"

    Any thoughts?
    blaine, Jul 25, 2007
    #1
    1. Advertising

  2. blaine

    dohzya Guest

    Le mercredi 25 juillet 2007 à 21:57 +0900, blaine a écrit :
    > I"m reading in a YAML file using the yaml library in Ruby. My YAML
    > looks something like this:
    >
    > config:
    > value1: Something here
    > value2: Something else
    >
    > As I loop from the YAML like:
    >
    > config["config"].each { |key, value| }
    >
    > How could I set the key as an instance variable with the value of the
    > value of the key... resulting in:
    >
    > @value1 = "Something here"
    > @value2 = "Something else"
    >
    > Any thoughts?
    >


    You should play with Object#instance_variable_set(name, value)

    --
    Etienne Vallette d'Osia
    dohzya, Jul 25, 2007
    #2
    1. Advertising

  3. blaine

    dohzya Guest

    Just an example to explain my mind :

    ---
    # some representation of your data
    config = {'config' => {'value1' => 'Something here', 'value2' =>
    'Something else'}}

    class A
    def m config
    config["config"].each { |key, value|
    # attr_accessor key (optional)
    self.class.instance_eval {attr_accessor key.to_s}
    # @key = value
    instance_variable_set "@#{key}", value
    }
    end
    end

    # test
    a = A.new
    a.m config
    p a.value2
    ---

    you can also specify if you want an attribute readable and/or writable

    --
    Etienne Vallette d'Osia
    dohzya, Jul 25, 2007
    #3
  4. blaine

    blaine Guest

    Your example really showed me, thank you so much Etienne. You have
    really helped me understand it much better by your example.

    --
    Tim Knight


    On Jul 25, 9:32 am, dohzya <> wrote:
    > Just an example to explain my mind :
    >
    > ---
    > # some representation of your data
    > config = {'config' => {'value1' => 'Something here', 'value2' =>
    > 'Something else'}}
    >
    > class A
    > def m config
    > config["config"].each { |key, value|
    > # attr_accessor key (optional)
    > self.class.instance_eval {attr_accessor key.to_s}
    > # @key = value
    > instance_variable_set "@#{key}", value
    > }
    > end
    > end
    >
    > # test
    > a = A.new
    > a.m config
    > p a.value2
    > ---
    >
    > you can also specify if you want an attribute readable and/or writable
    >
    > --
    > Etienne Vallette d'Osia
    blaine, Jul 25, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    478
    red floyd
    Nov 10, 2011
  2. Steven Hirsch

    Iterating over a hash of hash of hashes

    Steven Hirsch, Aug 19, 2008, in forum: Ruby
    Replies:
    0
    Views:
    142
    Steven Hirsch
    Aug 19, 2008
  3. Replies:
    5
    Views:
    106
  4. PerlFAQ Server
    Replies:
    0
    Views:
    124
    PerlFAQ Server
    Jan 10, 2011
  5. Replies:
    5
    Views:
    256
Loading...

Share This Page