From: ara.t.howard@... Date: 2006-05-11T14:55:49+09:00 Subject: Re: plz help with binding On Thu, 11 May 2006, Sergey Volkov wrote: > Now I'm trying to create instance-var-less storage: > # > class C > def self.arr_meth( sym ) > arr = [] > define_method sym.to_sym, lambda{ arr } > end > arr_meth :arm > end > ca = C.new; ca.arm << 10 > cb = C.new; cb.arm << 20 > p ca.arm #-> [10, 20] > p cb.arm #-> [10, 20] > p( ca.arm.eql?( cb.arm ) ) #-> true > # > Why ca.arm, cb.arm share same local variable? > Is it bug or feature? because, in effect, this is what you've done class C ARR = [] def arm() lambda{ ARM } end end there are a lot of ways to do this but you want something like: harp:~ > cat a.rb class C def self.arr_meth am = nil @arr_meths ||= [] @arr_meths << am.to_s if am @arr_meths end arr_meth :arm def initialize klass = self.class singleton_klass = class << self; self; end klass.arr_meth.each do |m| singleton_klass.module_eval do arr = [] define_method m, lambda{ arr } end end end end ca = C.new; ca.arm << 10 cb = C.new; cb.arm << 20 p ca.arm #-> [10] p cb.arm #-> [20] p( ca.arm.eql?( cb.arm ) ) #-> false harp:~ > ruby a.rb [10] [20] false neat idea btw. hth. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama