From: Robert Klemme Date: 2004-11-15T21:38:20+09:00 Subject: Re: OpenStruct#update ? "Florian Gross" schrieb im Newsbeitrag news:2vrj8eF2oj365U1@uni-berlin.de... > trans. (T. Onoma) wrote: > > > Sure. Okay first, basically what I was doing, given object o and data in hash > > h: > > > > h.each do |k,v| > > o.instance_variable_set("@#{k}", v) > > o.instance_eval <<-EOS > > def #{k}; @#{k}; end > > def #{k}=(x); @#{k}=x; end > > EOS > > end > > h.each do |k,v| > class << o; self; end.send(:attr_accessor, k) > o.k = v > end Are you sure, this works? IMHO this is more efficient: class << o; self; end.send(:attr_accessor, *h.keys) h.each do |k,v| o.send("#{k}=", v) end But it would be even better to check for existing methods in order to not overwrite existing methods: cl = class << o; self; end im = cl.instance_methods h.each do |k,v| cl.send(:attr_reader, k) unless im.include?(k.to_s) cl.send(:attr_writer, k) unless im.include?("#{k}=") o.send("#{k}=", v) end Kind regards robert