# Wish list item: Method#to\_proc

**URL:** <https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919>\
**Category:** Ruby\
**Created:** [November 23, 2008, 12:26am UTC](https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919 "2008-11-23T00:26:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [November 23, 2008, 12:26am UTC](https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919/1 "2008-11-23T00:26:23Z")

</div>

would this be useful/theoretically possible?  
UnboundMethod#to\_proc  
Thanks!  
-=R

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [November 23, 2008, 12:55am UTC](https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919/2 "2008-11-23T00:55:38Z")

</div>

On Nov 22, 6:22 pm, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## would this be useful/theoretically possible? UnboundMethod#to\_proc Thanks! -=R
> 
> Posted viahttp://www.ruby-forum.com/.

I put together an implementation here:  
[rebound/lib/rebound.rb at master · nakajima/rebound · GitHub](http://github.com/nakajima/rebound/tree/master/lib%2Frebound.rb#L11-13).  
Not great, since it relies on Ruby2Ruby, but it worked for what I  
needed, which was just to see if I could do it.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [November 23, 2008, 2:58am UTC](https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919/3 "2008-11-23T02:58:24Z")

</div>

On Nov 22, 2008, at 4:22 PM, Roger P. wrote:

> would this be useful/theoretically possible?  
> UnboundMethod#to\_proc  
> Thanks!  
> -=R

cfp:~ \> cat a.rb  
class UnboundMethod  
def to\_proc this = self  
bind(self).to\_proc  
end  
end

module M  
def m  
42  
end  
end

include M

p M.instance\_method(:m)

p M.instance\_method(:m).to\_proc(self).call

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

the issues is, without binding, what is ‘self’ ??? once bound,  
to\_proc exists.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [November 23, 2008, 6:10am UTC](https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919/4 "2008-11-23T06:10:45Z")

</div>

On Nov 22, 8:52 pm, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> bind(self).to_proc
> 
> ```
> 
> ## a @[http://codeforpeople.com/](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

The main issue with this approach is that instances of UnboundMethod  
can only be bound to objects of the same class on which the method was  
originally defined. That’s why I used Ruby2Ruby.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [November 23, 2008, 11:51am UTC](https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919/5 "2008-11-23T11:51:14Z")

</div>

On Sun, Nov 23, 2008 at 2:52 AM, ara.t.howard  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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 ☹

> the issues is, without binding, what is ‘self’ ??? once bound, to\_proc  
> exists.

It could be provided at call time, see example above. I kinda like the  
idea.

Cheers  
Robert

–  
Ne baisse jamais la tÃªte, tu ne verrais plus les Ã©toiles.

Robert D. 😉

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [November 23, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919/6 "2008-11-23T18:17:16Z")

</div>

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

> 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/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [November 23, 2008, 3:58am UTC](https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919/7 "2008-11-23T03:58:51Z")

</div>

> cfp:~ \> ruby a.rb  
> #\<UnboundMethod: M#m\>  
> 42
> 
> the issues is, without binding, what is ‘self’ ??? once bound,  
> to\_proc exists.

Cool, though I’ll admit I don’t understand exactly everything that goes  
on in there 🙂

My real goal is to be able to share methods from one class to another.

I guess one could “share” methods from class to class [if the method  
originated from a module] by searching ancestor modules for a method and  
then ‘cherry picking’ it from the ancestor and including it [?]  
-=R

[http://eigenclass.org/hiki.rb?cmd=view&p=Rename+and+reject+methods+from+included+modules&key=eiffel](http://eigenclass.org/hiki.rb?cmd=view&p=Rename+and+reject+methods+from+included+modules&key=eiffel)

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 1, 2008, 8:59pm UTC](https://www.ruby-forum.com/t/wish-list-item-method-to-proc/153919/8 "2008-12-01T20:59:29Z")

</div>

Ara Howard wrote:

> On Nov 23, 2008, at 3:46 AM, Robert D. wrote:
> 
> > 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:~ \> ruby a.rb  
> #\<UnboundMethod: Object#m\>  
> 42

Wow that is dang cool.  
Next challenge:  
getting a method to transfer “easily” from one class to another [not  
directly related classes]. I guess you could do it if the method  
originated from a module–is that the only way?

Ex:

module AddToString  
def yoyo  
‘within string func’  
end

end

class String  
include AddToString  
end

class Object

# attempt to find and ‘borrow’ a method from another class

# in 1.8, method\_name should be a string, in 1.9, not sure

def borrow\_and\_run\_method(from\_this, method\_name\_desired)  
module\_where\_found = nil  
for ancestor in from\_this.ancestors do  
module\_where\_found ||= ancestor if ancestor.class == Module &&  
ancestor.instance\_methods.include?(method\_name\_desired)  
end  
raise unless module\_where\_found  
m = Module.new  
m.module\_eval { include module\_where\_found}  
m.instance\_methods.each{|method\_name| m.module\_eval{  
undef\_method(method\_name) unless method\_name == method\_name\_desired } }  
# todo: double check we’re not overwriting anything…  
self.extend m  
self.send method\_name\_desired  
end  
end

class NoYoYo; end

print NoYoYo.new.borrow\_and\_run\_method(String, ‘yoyo’) # prints ‘within  
string func’
