Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Creating instance variables while looping over a hash

Reply
Thread Tools

Creating instance variables while looping over a hash

 
 
blaine
Guest
Posts: n/a
 
      07-25-2007
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?


 
Reply With Quote
 
 
 
 
dohzya
Guest
Posts: n/a
 
      07-25-2007
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


 
Reply With Quote
 
 
 
 
dohzya
Guest
Posts: n/a
 
      07-25-2007
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


 
Reply With Quote
 
blaine
Guest
Posts: n/a
 
      07-25-2007
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 <doh...@gmail.com> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Adding values to anon hash of hash while looping... adamomitcheney@kiwis.co.uk Perl Misc 5 09-02-2005 04:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57