# Creating instance variables while looping over a hash

**URL:** <https://www.ruby-forum.com/t/creating-instance-variables-while-looping-over-a-hash/196407>\
**Category:** Ruby\
**Created:** [July 25, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/creating-instance-variables-while-looping-over-a-hash/196407 "2007-07-25T14:59:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [July 25, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/creating-instance-variables-while-looping-over-a-hash/196407/1 "2007-07-25T14:59:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [July 25, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/creating-instance-variables-while-looping-over-a-hash/196407/2 "2007-07-25T15:14:14Z")

</div>

Le mercredi 25 juillet 2007 à 21:57 +0900, blaine a écrit :

> 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)

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [July 25, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/creating-instance-variables-while-looping-over-a-hash/196407/3 "2007-07-25T15:33:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [July 25, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/creating-instance-variables-while-looping-over-a-hash/196407/4 "2007-07-25T16:08:22Z")

</div>

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

–  
Tim Knight
