From: Florian Gross Date: 2005-02-05T00:30:38+09:00 Subject: Re: method_missing question Joe Van Dyk wrote: > Can someone complete the psuedo-code in SomeObject#method_missing for > me? Or suggest a better way of doing this? The attributes for > SomeObject are, in my application, are generated from a configuration > file. > > Thanks > Joe > > class SomeObject > def initialize > @attributes = { :x_pos => 20, :y_pos => 30, :z_pos => 40 } > end Perhaps you could do it simpler by changing this logic? I was thinking along the lines of def initialize() { :x_pos => 20, :y_pos => 30, :z_pos => 40 }.each do |key, value| instance_variable_set("@#{key}", value) class << self; self; end.send(:attr_accessor, key) end end But that won't quite work when the field set can change after initialize has been called. When you know all the keys upfront you can also move the accessors to the class definition.