From: Markus Jais Date: 2002-06-03T23:58:11+09:00 Subject: Re: dynamic attr_accessor?? thanks for your tip. does one of the two possible solutions have any significant advantages over the other or is this just a matter of style?? is one more like "the ruby way" ?? markus Yukihiro Matsumoto wrote: > Hi, > > In message "dynamic attr_accessor??" > on 02/06/03, Markus Jais 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 > end.module_eval{attr_accessor name} > end > end > > matz.