From: Florian Frank Date: 2002-12-17T19:43:15+09:00 Subject: Re: standard accessor for List attribute On 2002-12-17 18:25:24 +0900, Det wrote: > Is there a way to do something like: > > class MyClass > attrList_accessor :aRelation > end > > to get the above ? > > Or is thought about putting something like that > into future versions of Ruby ? You could extend Ruby like that: class Class def attr_list_accessor(*symbols) symbols.each do |s| class_eval <<-EOS def add_#{s}(elm) (@#{s} ||= []) << elm end def each_#{s}(&block) (@#{s} ||= []).each(&block) end EOS end end end class Test attr_list_accessor :foo, :bar end t = Test.new t.add_foo "foo" t.add_bar "bar" t.add_bar "baz" puts "Foo:" t.each_foo { |e| puts e } puts "Bar:" t.each_bar { |e| puts e } -- 6) "Debugging? Klingons do not debug. Our software does not coddle the weak." -- "Top 12 things likely to be overheard if you had a Klingon Programmer"