From: Alan Chen Date: 2001-10-31T02:28:04+09:00 Subject: [ruby-talk:23859] Re: RCR: Fun with attribute shortcuts solves RCR #3 and more On Wed, Oct 31, 2001 at 02:14:38AM +0900, Dan Schmidt wrote: > Gunnar Andersson writes: > > | # file: AttrInitializer > | class Module > | def attr_initializer(*attrs) > | alist = plist = "" > | attrs.each do |v| > | alist += "@#{v}," # >> @var1,@var2,@var3, ... > | plist += "#{v}," # >> var1,var2,var3, ... > | end > | alist.chop!; plist.chop! # Remove last comma > | class_eval "def initialize(#{plist}); #{alist} = #{plist}; end" > | end > | end > > Perhaps the generated initialize method could also check for the > presence of a initialize_more (or a better name, hopefully) method > and call it if it exists. I think I might like to invert that and have attr_initializer generate a initialize_attrs call which can be manually used in the initialize call. Something like this: class Foo attr_initializer :var1, :var2 def initialize # other init stuff ... initialize_attrs # other init stuff ... end end > That way you could set a bunch of member variables automatically, and > then continue by doing other initialization such as setting other > derived variables. > > -- > http://www.dfan.org -- Alan Chen President and Lead Developer Digikata LLC alan@digikata.com http://digikata.com