# Questions re Method#to\_proc

**URL:** <https://rubytalk.org/t/questions-re-method-to-proc/26718>\
**Category:** ruby-talk\
**Created:** [18 April 2006 08:39 UTC](https://rubytalk.org/t/questions-re-method-to-proc/26718 "2006-04-18T08:39:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eli\_Bendersky](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eli\_Bendersky](https://rubytalk.org/u/Eli_Bendersky)\
**Post date:** [18 April 2006 08:39 UTC](https://rubytalk.org/t/questions-re-method-to-proc/26718/1 "2006-04-18T08:39:51Z")

</div>

Hello all,

Some things are unclear to me about Method#to\_proc. I understand the  
following:

plus = 12.method("+")  
p plus.call(13) # prints 25

newplus = plus.unbind.bind(20)  
p newplus.call(13) # prints 33

Although the usefulness of this eludes me 🙂

However, this:

plus\_proc = plus.to\_proc  
p plus\_proc.call(10) # prints 10

Is unclear...  
1) How does it work ?  
2) What does it mean for a Proc to be bound to an object ?  
3) Can someone provide an example where it is useful ?  
4) Have the 'bind' and 'unbind' methods of Method / UnboundMethod  
anything in common with Proc#binding or the Binding class ?  
5) Have Proc#binding and the Binding class anything in common ? How  
about Kernel#binding ?

Additionally, the following, IMHO demostrates a very surprising and  
unnatural behavior of to\_proc:

def foo(arr)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Got an array with #{arr.length} elements"  
end

# works correctly  
foo([4, 5, 6])

foo\_proc = method(:foo).to\_proc

# throws an ArgumentError: 3 for 1  
foo\_proc.call([4, 5, 6])

# works correctly  
foo\_proc.call([[4, 5, 6]])

The Proc created by to\_proc is obviously different from the original  
method, since it "folds" its arguments into an array. I guess there  
will be even more problems when the method receives more than one array  
as an argument.  
How should this be handled correctly ?

Thanks in advance

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

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [19 April 2006 15:28 UTC](https://rubytalk.org/t/questions-re-method-to-proc/26718/2 "2006-04-19T15:28:56Z")

</div>

Hi,

> **···**
>
> In message "Re: questions re Method#to\_proc" on Tue, 18 Apr 2006 17:39:51 +0900, Eli Bendersky \<eliben@gmail.com\> writes:
> 
> > Additionally, the following, IMHO demostrates a very surprising and  
> > unnatural behavior of to\_proc:
> > 
> > def foo(arr)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Got an array with #{arr.length} elements"  
> > end
> > 
> > # works correctly  
> > foo([4, 5, 6])
> > 
> > foo\_proc = method(:foo).to\_proc
> > 
> > # throws an ArgumentError: 3 for 1  
> > foo\_proc.call([4, 5, 6])
> > 
> > # works correctly  
> > foo\_proc.call([[4, 5, 6]])
> 
> This issue will be solved in the future version (1.9), which should  
> give you the following result:
> 
> Got an array with 3 elements  
> Got an array with 3 elements  
> Got an array with 1 elements
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Eli\_Bendersky](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eli\_Bendersky](https://rubytalk.org/u/Eli_Bendersky)\
**Post date:** [19 April 2006 17:14 UTC](https://rubytalk.org/t/questions-re-method-to-proc/26718/3 "2006-04-19T17:14:00Z")

</div>

Yukihiro Matsumoto wrote:

> \># works correctly  
> \>foo([4, 5, 6])  
> \>  
> \>foo\_proc = method(:foo).to\_proc  
> \>  
> \># throws an ArgumentError: 3 for 1  
> \>foo\_proc.call([4, 5, 6])  
> \>  
> \># works correctly  
> \>foo\_proc.call([[4, 5, 6]])
> 
> This issue will be solved in the future version (1.9), which should  
> give you the following result:
> 
> Got an array with 3 elements  
> Got an array with 3 elements  
> Got an array with 1 elements
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

Thanks, Matz.

By the way, when is 1.9 due ?

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

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [19 April 2006 23:01 UTC](https://rubytalk.org/t/questions-re-method-to-proc/26718/4 "2006-04-19T23:01:42Z")

</div>

Hi,

> **···**
>
> In message "Re: questions re Method#to\_proc" on Thu, 20 Apr 2006 02:14:00 +0900, Eli Bendersky \<eliben@gmail.com\> writes:
> 
> > By the way, when is 1.9 due ?
> 
> You can fetch it from CVS right now.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.
