# Convert

**URL:** <https://www.ruby-forum.com/t/convert/95833>\
**Category:** Ruby\
**Created:** [June 15, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/convert/95833 "2006-06-15T12:01:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [June 15, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/convert/95833/1 "2006-06-15T12:01:33Z")

</div>

Hello

I wonder if there is a simple way to convert all keys in a Hash to  
member variables in a Class without knowing the key names in the Hash  
beforehand.

example  
MyHash[“ada”] = “yes”  
MyHash[“beda”] = “no”  
MyHash[“foo”] = “bar”  
…  
…  
…  
MyHash[“whatever”]=‘whatever’

can be accessed via the point operator

myClass.ada  
myClass.beda  
myClass.foo  
…  
…  
…  
myClass.whatever

/Joakim

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [June 15, 2006, 12:46pm UTC](https://www.ruby-forum.com/t/convert/95833/2 "2006-06-15T12:46:05Z")

</div>

Joakim O. wrote:

> Hello
> 
> I wonder if there is a simple way to convert all keys in a Hash to  
> member variables in a Class without knowing the key names in the Hash  
> beforehand.

vars = {:foo =\> 101, :bar =\> 42, :baz =\> 1337}  
vars.each do |name, value|  
SomeClass.instance\_variable\_set("@#{name}", value)  
end

You could also use an OpenStruct.

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [June 15, 2006, 12:59pm UTC](https://www.ruby-forum.com/t/convert/95833/3 "2006-06-15T12:59:15Z")

</div>

Daniel S. wrote:

> end
> 
> You could also use an OpenStruct.

That won’t work, because it doesn’t create accessors.

This has been discussed recently a couple of times.

[Ruby Quiz - Hash to OpenStruct (#81)](http://rubyquiz.com/quiz81.html)

Also, the simple answer is this:

require ‘ostruct’  
obj = OpenStruct.new(hash)

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [June 15, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/convert/95833/4 "2006-06-15T13:26:30Z")

</div>

Dave B. wrote:

> > end
> > 
> > You could also use an OpenStruct.
> 
> That won’t work, because it doesn’t create accessors.

Ahh, didn’t see the last part of the post!

(Damn these trigger-happy fingers of mine!)

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [June 15, 2006, 1:16pm UTC](https://www.ruby-forum.com/t/convert/95833/5 "2006-06-15T13:16:13Z")

</div>

Thanks

/Joakim
