Creating instance variables while looping over a hash


#1

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?


#2

config[“config”].each {|key,value| instance_variable_set("@#{key}",
value)};

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

Adam


#3

Thank you so much Adam.

Tim