From: Florian Gross Date: 2005-03-30T04:59:46+09:00 Subject: Re: attr :? Glenn Smith wrote: > Here is James' example: > > A simple example > > # Meta-programming > # Add a public array property to an object instance > > class Meta > def self.has_many( sym ) > attr_tmp = "def #{sym.to_s} \n" > attr_tmp << " @#{sym.to_s} = [] unless @#{sym.to_s}\n" > attr_tmp << " @#{sym.to_s}; end" > eval ( attr_tmp ) > end > end And here it is without an eval: class Meta def self.has_many(sym) ivar = :"@#{sym}" define_method(sym.to_sym) do new_value = instance_variable_get(ivar) || [] instance_variable_set(ivar, new_value) new_value end end end