From: Dave Burt Date: 2006-05-22T00:39:01+09:00 Subject: Re: I call Ducktype Violation on #to_proc! :) Daniel Schierbeck wrote: > Dave Burt wrote: >> def to_proc >> proc {|*args| self.call(*args) } >> end > > Yes, that works (thanks!), but I think it's against Ruby spirit to > require a certain class for a relatively high-level thing like this. I > can see why there's not really any way around a string ultimately being > a String object, through #to_str or #to_s, but all that's really > required by the return value of #to_proc is an object that responds to > #call. You can actually use certain non-Proc objects: irb> def say_hello_to() yield "hello" end => nil irb> say_hello_to &method(:puts) hello => nil And you can also use a subclass of Proc. class CachedProc < Proc ... ... and your example would work, I think. Getting off-topic, if I may revisit your cached_proc again -- you can easily do without the class entirely. def cached_proc &block cache = Hash.new {|h, k| h[k] = block.call(*args) } proc {|*args| cache[args] } end Cheers, Dave