From: Dave Burt Date: 2006-05-21T08:56:24+09:00 Subject: Re: I call Ducktype Violation on #to_proc! :) Daniel Schierbeck wrote: > class CachedProc > def initialize(&proc) > @proc = proc > @cache = Hash.new{|cache, args| cache[args] = @proc.call(*args)} > end > > def call(*args) > @cache[args] > end > > alias_method :[], :call > > def to_proc > # must return a Proc... > @proc > end > ... > This is fine and all, but doesn't work with iterators and such: > > proc = cached_proc{|obj| expensive_operation(obj)} > [2, 5, 7, 4, 2, 8, 2].collect(&proc) > > Here, the caching doesn't work, because the CachedProc is converted to a > Proc. def to_proc proc {|*args| self.call(*args) } end Cheers, Dave