From: Mushfeq Khan Date: 2007-03-12T08:41:18+09:00 Subject: Re: Metaprogramming and class_eval ------=_Part_29255_18457659.1173656475896 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, You seem to have an extra .class in your modification to Class. You can get away with this: class Class def my_attr_accessor( *args ) self.class_eval do attr_accessor *args end end end Explanation: When you're invoking my_attr_accessor from within the class def of MyNewClass, 'self' is the class object MyNewClass (an instance of Class). You need to call class_eval on this object. When the block passed into class_eval is executed, 'self' is again the class object MyNewClass. This is what allows you to access the private 'attr_accessor' method. Mushfeq. On 3/11/07, Pedro Del Gallego wrote: > > Hi > > Whats the differents between this three classes. Why the > my_attr_accesor dosent create getters and setter clases. > > > Thanks in advance > > #---------------------------------------------------------------------- > # > #---------------------------------------------------------------------- > > class Class > def my_attr_accessor( *args ) > args.each do |name| > self.class.class_eval do > attr_accessor :"#{name}" > end > end > end > end > => nil > > class MyNewClass > my_attr_accessor :id, :diagram, :telegram > end > => [:id, :diagram, :telegram] > > class MyClass > attr_accessor :id, :diagram, :telegram > end > => nil > > class Another_class > def initialize > ["id","diagram","telegram"].each do |name| > self.class.class_eval do > attr_accessor :"#{name}" > end > end > end > end > => nil > > mnc = MyNewClass.new > => # > mc = MyClass.new > => # > ac = Another_class.new > => # > > ac.diagram > => nil > mc.diagram > > > -- > ------------------------------------- > Pedro Del Gallego > > Email : pedro.delgallego@gmail.com > > ------=_Part_29255_18457659.1173656475896--