From: Park Heesob Date: 2002-06-04T18:39:18+09:00 Subject: Re: dynamic attr_accessor again using a hash Hi, "Markus Jais" wrote in message news:hQ_K8.5$VC3.915@news.ecrc.de... > > > ######### > class Cla > def initialize(hash) > hash.keys.each do |key| > type.send(:attr_accessor, key) > end > > hash.each do |key, value| > self.instance_eval " > @#{key}=value > " > end > end > > end > hash = { "uno" => "one", "dos" => "two"} > c = Cla.new(hash) > > puts c.uno > puts c.dos > > ########## > > I tryed to avoid "eval" like Holden Glova recommended but > I found no way to avoid "eval" for initializing the instance > variables. I am quite content with this solution, but > if there is an even better one, please let me know. > thanks! > How about this? ========================================== class Cla def initialize(hash) hash.each do |key,value| type.send(:attr_accessor, key) send(key+'=', value) end end end hash = { "uno" => "one", "dos" => "two"} c = Cla.new(hash) =========================================== Park Heesob.