From: Phrogz Date: 2006-10-17T12:45:10+09:00 Subject: Re: dynamically assigning instance variables (redux) Bil Kleb wrote: > Is there a way to dynamically assign instance variables? [...snip...] > Actually, I'd like the assignment and make them attr_readers too. class Foo def initialize( hash={} ) hash.each{ |key,value| var_name = "@#{key}" instance_variable_set( var_name, value ) singleton_class = class << self; self; end singleton_class.class_eval{ define_method( key ){ instance_variable_get( var_name ) } define_method( "#{key}=" ){ |new_value| instance_variable_set( var_name, new_value ) } } } end end f = Foo.new :name=>'whee', :age=>12 p f, f.age #=> # #=> 12 f.age=32 p f #=> #