On Nov 23, 2008, at 3:46 AM, Robert Dober wrote:

> On Sun, Nov 23, 2008 at 2:52 AM, ara.t.howard  
> <ara.t.howard / gmail.com>wrote:
>
>>
>> I guess you meant
> bind( this ).to_proc
>
>>
>> p M.instance_method(:m).to_proc(self).call
>
> but I guess OP rather wanted
>    M.i_m(:m).to_proc.call( self )
>
> which on a quick thought seems impossible to have :-(

na, just have to perform the late binding in the caller's stead


cfp:~ > cat a.rb
class UnboundMethod
   def to_proc
     lambda do |this, *a|
       bind(this).to_proc.call(*a)
     end
   end
end

class Object
   def m
     42
   end
end


p Object.instance_method(:m)
p Object.instance_method(:m).to_proc.call(self)



cfp:~ > ruby a.rb
#<UnboundMethod: Object#m>
42
cfp:~ >


other permutations, such as


cfp:~ > cat a.rb
class UnboundMethod
   def to_proc
     lambda{|*a| bind(this=self).to_proc.call(*a)}
   end
end

class Object
   def m() 42 end
end

p Object.instance_method(:m)
p Object.instance_method(:m).to_proc.call


cfp:~ > ruby a.rb
#<UnboundMethod: Object#m>
42


which perform automatic late binding of self are also possible/ 
preferrable

of course there is the separate issue of what objects an unbound  
method can be attached to - but i think an orthogonal solution to  
that, plus something simple like the above, can handle most of the  
cases.  also, ruby19 provides #owner in UnboundMethod, which is useful  
for this case i think.

cheers.

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama