From: Dan Doel Date: 2003-09-10T07:47:31+09:00 Subject: Re: Eval Other ways if you can't use a struct for some reason. Method2 not recommended (unless you check if the accessor has been defined yourself). - Dan class Method1 def add_attr(name, val) class << self self end.class_eval { attr_accessor name } send("#{name}=", val) end end class Method2 def initialize @dict = Hash.new {nil} end def method_missing(sym, *args) str = sym.to_s /(.*)(=?)/ =~ str if $2.empty? @dict[$1.intern] else @dict[$1.intern] = args[0] end end def add_attr(name, val) @dict[name.intern] = val end end obj = Method1.new obj.add_attr("foo", "bar") obj2 = Method2.new obj2.add_attr("baz", "frob") p obj.foo p obj2.baz p obj2.any_undefined_method_name