From: Dave Burt Date: 2006-06-15T19:55:14+09:00 Subject: Re: convert Daniel Schierbeck wrote: > Joakim Oman 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. That won't work, because it doesn't create accessors. This has been discussed recently a couple of times. http://rubyquiz.com/quiz81.html Also, the simple answer is this: require 'ostruct' obj = OpenStruct.new(hash) Cheers, Dave