Dave Burt wrote:
> 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

But is that because of #to_proc the the method object, or is Ruby 
treating method objects different?

> 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

Yeah, that simplifies things quite a bit, thanks mate!


Daniel