From: David Naseby Date: 2004-03-01T13:47:19+09:00 Subject: Re: turn hash into object variables? >From: Ruby Baby [mailto:ruby@hitmedia.com] >I know beautiful Ruby has an easier way to do this: > > >class SomeClass > def initialize(someHash) > @name = someHash['name'] > @address = someHash['address'] > @blah = someHash['blah'] > # etc for many key=>value pairs ... > end >end > > >How can I make it do that for every key in the hash? > class SomeClass def initialize( h ) h.each do | key, val | instance_variable_set( "@#{key}".intern, val ) end end end David Naseby http://homepages.ihug.com.au/~naseby/