# Questions re Method#to\_proc

**URL:** <https://www.ruby-forum.com/t/questions-re-method-to-proc/94026>\
**Category:** Ruby\
**Created:** [April 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/questions-re-method-to-proc/94026 "2006-04-18T10:39:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [April 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/questions-re-method-to-proc/94026/1 "2006-04-18T10:39:42Z")

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

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [April 19, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/questions-re-method-to-proc/94026/2 "2006-04-19T17:30:52Z")

</div>

Hi,

In message “Re: questions re Method#to\_proc”  
on Tue, 18 Apr 2006 17:39:51 +0900, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Additionally, the following, IMHO demostrates a very surprising and  
|unnatural behavior of to\_proc:  
|  
|def foo(arr)  
| 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

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [April 19, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/questions-re-method-to-proc/94026/3 "2006-04-19T19:13:51Z")

</div>

Yukihiro M. 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
> 
> ```
> matz.
> 
> ```

Thanks, Matz.

By the way, when is 1.9 due ?

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [April 20, 2006, 1:04am UTC](https://www.ruby-forum.com/t/questions-re-method-to-proc/94026/4 "2006-04-20T01:04:00Z")

</div>

Hi,

In message “Re: questions re Method#to\_proc”  
on Thu, 20 Apr 2006 02:14:00 +0900, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|By the way, when is 1.9 due ?

You can fetch it from CVS right now.

```
						matz.

```
