Forum: Ruby on Rails Creating instance variables while looping over a hash

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
blaine (Guest)
on 2007-07-25 10:41
(Received via mailing list)
I"m reading in a YAML file using the yaml library in Ruby.  My YAML
looks something like this:

     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?
Adam C. (Guest)
on 2007-07-25 22:07
(Received via mailing list)
config["config"].each {|key,value| instance_variable_set("@#{key}",

you'll probably want to remove spaces and convert odd chars in the key
to make sure it follows instance variable naming guidelines.

blaine (Guest)
on 2007-07-26 09:15
(Received via mailing list)
Thank you so much Adam.

This topic is locked and can not be replied to.