From: Sean O'Halpin Date: 2005-10-18T07:09:57+09:00 Subject: Re: declaratively caching results of a method On 10/17/05, Daniel Berger wrote: > Eh? If you memoize in initialize, why wouldn't another instance of Foo get the > benefit? Try it: class Foo include Memoize def initialize memoize :foo end def foo(*args) puts "calculating #{self}.foo(#{args.map{|x| x.inspect}.join(',')})" args.inject(0) {|sum, x| sum + x} end end f = Foo.new puts f.foo(2) puts f.foo(2) g = Foo.new puts g.foo(2) __END__ calculating #.foo(2) 2 2 calculating #.foo(2) 2 It's because you're defining the memoized method on the singleton class: (class << self; self; end).class_eval do ... end Regards, Sean