Hi --

On Sun, 19 Mar 2006, Trans wrote:

> So here's the Functor class:
>
>  class Functor < BasicObject
>    def initialize(*obj, &func)
>      @obj = obj
>      @func = func
>    end
>    def method_missing(op, *args)
>      if @obj.empty?
>        @func.call(op, *args)
>      else
>        @func.call(op, *(@obj + args))
>      end
>    end
>  end
>
> Anyone who has seen the older version of this cllas will notice that I
> have added the *obj parameter. This allows the Functor to act like a
> simple delegator too. But strictly speaking it isn't at all necessary
> since one could just use the object(s) passed to the initialize
> directly in the block. Eg.
>
>  foo = 1
>  Functor.new(foo) { |op, obj, *args| obj.send(op, *args) }
>
> is the exact same as
>
>  foo = 1
>  Functor.new { |op, *args| foo.send(op, *args) }
>
> The only time this really is a little more handy is when passing
> 'self', because of the ambiguity between contexts. Eg.
>
>  Functor.new(self) { |op, obj, *args| obj.send(op, *args) }
>
> vs.
>
>  slf = self
>  Functor.new { |op, *args| slf.send(op, *args) }

You shouldn't need to do that, though; self will be preserved from the
context of the block's creation:

     class C
     def f(&b)
       b.call
     end
   end

   C.new.f { puts self }  # main


David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black