Hi,

In message "dynamic attr_accessor??"
    on 02/06/03, Markus Jais <mjais / web.de> writes:

|I like to create methods for my class dynamically based on
|strings I pass to initialize

 a) attr_accessor is a method of a class
 b) attr_accessor takes names of accessors
 c) attr_accessor just defines two methods

So the answer should be

easier one:

class Cl
   def initialize(name)
     @name = name
     self.instance_eval "
       def #{name}; @#{name}; end
       def #{name}=(val); @#{name}=val; end
     "
   end
end

trickier one:

class Cl
   def initialize(name)
     @name = name
     class <<self
       self
     end.module_eval{attr_accessor name}
   end
end

							matz.