# Wish list item: Method#to\_proc

**URL:** <https://rubytalk.org/t/wish-list-item-method-to-proc/50321>\
**Category:** ruby-talk\
**Created:** [22 November 2008 23:22 UTC](https://rubytalk.org/t/wish-list-item-method-to-proc/50321 "2008-11-22T23:22:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [22 November 2008 23:22 UTC](https://rubytalk.org/t/wish-list-item-method-to-proc/50321/1 "2008-11-22T23:22:09Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Pat\_Nakajima](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@Pat\_Nakajima](https://rubytalk.org/u/Pat_Nakajima)\
**Post date:** [22 November 2008 23:50 UTC](https://rubytalk.org/t/wish-list-item-method-to-proc/50321/2 "2008-11-22T23:50:52Z")

</div>

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%5C).  
Not great, since it relies on Ruby2Ruby, but it worked for what I  
needed, which was just to see if I could do it.

> **···**
>
> On Nov 22, 6:22 pm, Roger Pack \<rogerpack2...@gmail.com\> wrote:
> 
> > would this be useful/theoretically possible?  
> > UnboundMethod#to\_proc  
> > Thanks!  
> > -=R  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [23 November 2008 01:52 UTC](https://rubytalk.org/t/wish-list-item-method-to-proc/50321/3 "2008-11-23T01:52:26Z")

</div>

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

module M  
&nbsp;&nbsp;&nbsp;def m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42  
&nbsp;&nbsp;&nbsp;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/)

> **···**
>
> On Nov 22, 2008, at 4:22 PM, Roger Pack wrote:
> 
> > would this be useful/theoretically possible?  
> > UnboundMethod#to\_proc  
> > Thanks!  
> > -=R
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [23 November 2008 02:54 UTC](https://rubytalk.org/t/wish-list-item-method-to-proc/50321/4 "2008-11-23T02:54:36Z")

</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)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Pat\_Nakajima](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@Pat\_Nakajima](https://rubytalk.org/u/Pat_Nakajima)\
**Post date:** [23 November 2008 05:05 UTC](https://rubytalk.org/t/wish-list-item-method-to-proc/50321/5 "2008-11-23T05:05:54Z")

</div>

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.

> **···**
>
> On Nov 22, 8:52 pm, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:
> 
> > On Nov 22, 2008, at 4:22 PM, Roger Pack 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/)  
> > --  
> > we can deny everything, except that we have the possibility of being  
> > better. simply reflect on that.  
> > h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [23 November 2008 10:46 UTC](https://rubytalk.org/t/wish-list-item-method-to-proc/50321/6 "2008-11-23T10:46:30Z")

</div>

> I guess you meant

bind( this ).to\_proc

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

but I guess OP rather wanted  
&nbsp;&nbsp;&nbsp;&nbsp;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

> **···**
>
> On Sun, Nov 23, 2008 at 2:52 AM, ara.t.howard \<ara.t.howard@gmail.com\>wrote:
> 
> --  
> Ne baisse jamais la tête, tu ne verrais plus les étoiles.
> 
> Robert Dober 😉

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [23 November 2008 17:12 UTC](https://rubytalk.org/t/wish-list-item-method-to-proc/50321/7 "2008-11-23T17:12:14Z")

</div>

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

cfp:~ \> cat a.rb  
class UnboundMethod  
&nbsp;&nbsp;&nbsp;def to\_proc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lambda do |this, \*a|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bind(this).to\_proc.call(\*a)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

class Object  
&nbsp;&nbsp;&nbsp;def m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42  
&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;def to\_proc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lambda{|\*a| bind(this=self).to\_proc.call(\*a)}  
&nbsp;&nbsp;&nbsp;end  
end

class Object  
&nbsp;&nbsp;&nbsp;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/)

> **···**
>
> 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  
> > &nbsp;&nbsp;&nbsp;M.i\_m(:m).to\_proc.call( self )
> > 
> > which on a quick thought seems impossible to have ☹
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [1 December 2008 19:53 UTC](https://rubytalk.org/t/wish-list-item-method-to-proc/50321/8 "2008-12-01T19:53:58Z")

</div>

Ara Howard wrote:

> > but I guess OP rather wanted  
> > &nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;def yoyo  
&nbsp;&nbsp;&nbsp;&nbsp;'within string func'  
&nbsp;&nbsp;end

end

class String  
&nbsp;&nbsp;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)  
&nbsp;&nbsp;&nbsp;&nbsp;module\_where\_found = nil  
&nbsp;&nbsp;&nbsp;&nbsp;for ancestor in from\_this.ancestors do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module\_where\_found ||= ancestor if ancestor.class == Module &&  
ancestor.instance\_methods.include?(method\_name\_desired)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;raise unless module\_where\_found  
&nbsp;&nbsp;&nbsp;&nbsp;m = Module.new  
&nbsp;&nbsp;&nbsp;&nbsp;m.module\_eval { include module\_where\_found}  
&nbsp;&nbsp;&nbsp;&nbsp;m.instance\_methods.each{|method\_name| m.module\_eval{  
undef\_method(method\_name) unless method\_name == method\_name\_desired } }  
&nbsp;&nbsp;&nbsp;&nbsp;# todo: double check we're not overwriting anything...  
&nbsp;&nbsp;&nbsp;&nbsp;self.extend m  
&nbsp;&nbsp;&nbsp;&nbsp;self.send method\_name\_desired  
end  
end

class NoYoYo; end

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

> **···**
>
> > On Nov 23, 2008, at 3:46 AM, Robert Dober wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
