# Understanding the behavior of Method#to\_proc

**URL:** <https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197>\
**Category:** Ruby\
**Created:** [April 19, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197 "2006-04-19T16:25:22Z")\
**Posts on this page:** 9\
**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 19, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197/1 "2006-04-19T16:25:22Z")

</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:03pm UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197/2 "2006-04-19T17:03:09Z")

</div>

> > > > > “E” == Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

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

E\> Is unclear…  
E\> 1) How does it work ?

not like you think 🙂

moulon% ruby -e ‘p 2.method(“+”).to\_proc.call(10)’  
12  
moulon%

E\> 4) Have the ‘bind’ and ‘unbind’ methods of Method / UnboundMethod  
E\> anything in common with Proc#binding or the Binding class ?

well #bind associate a Binding with an UnboundMethod

E\> 5) Have Proc#binding and the Binding class anything in common ? How  
E\> about Kernel#binding ?

moulon% ruby -e ‘p = proc { }; p p.binding.class’  
Binding  
moulon%

moulon% ruby -e ‘p binding.class’  
Binding  
moulon%

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

good question 🙂

Guy Decoux

---

<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:21pm UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197/3 "2006-04-19T17:21:42Z")

</div>

Hi,

In message “Re: understanding the behavior of Method#to\_proc”  
on Wed, 19 Apr 2006 23:25:34 +0900, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|plus = 12.method(“+”)  
|p plus.call(13) # prints 25

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

Which version of Ruby are you using? My 1.8.4 gives me 22.

```
						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 20, 2006, 1:07am UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197/4 "2006-04-20T01:07:10Z")

</div>

Hi,

In message “Re: understanding the behavior of Method#to\_proc”  
on Thu, 20 Apr 2006 02:19:41 +0900, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Still, I don’t quite understand what does it mean for a Proc to be bound  
|to an object ? Does it have some access to the object’s internals, like  
|the method did ? When is this useful ?

Still I don’t quite understand what you expect for a Proc not to be  
bound to an object in this case. It was only reasonable design for  
me.

```
						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 20, 2006, 1:55pm UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197/5 "2006-04-20T13:55:28Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: understanding the behavior of Method#to\_proc”  
> on Thu, 20 Apr 2006 02:19:41 +0900, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> writes:
> 
> |Still, I don’t quite understand what does it mean for a Proc to be bound  
> |to an object ? Does it have some access to the object’s internals, like  
> |the method did ? When is this useful ?
> 
> Still I don’t quite understand what you expect for a Proc not to be  
> bound to an object in this case. It was only reasonable design for  
> me.
> 
> ```
> matz.
> 
> ```

Matz,

I’m not questioning your design at all - I ask out of sheer curiosity  
what this language construct (Proc bound to an object) does, because it  
isn’t documented anywhere (please correct me if I’m mistaken). Moreover,  
I’ll be happy to see an example of when this can be useful in Ruby  
coding.

That’s all I’m asking, really 🙂

Eli

---

<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:19pm UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197/6 "2006-04-19T19:19:32Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: understanding the behavior of Method#to\_proc”  
> on Wed, 19 Apr 2006 23:25:34 +0900, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> writes:
> 
> |plus = 12.method(“+”)  
> |p plus.call(13) # prints 25
> 
> |plus\_proc = plus.to\_proc  
> |p plus\_proc.call(10) # prints 10
> 
> Which version of Ruby are you using? My 1.8.4 gives me 22.
> 
> ```
> matz.
> 
> ```

You are right, I made a mistake. It prints 22 for me too.

Still, I don’t quite understand what does it mean for a Proc to be bound  
to an object ? Does it have some access to the object’s internals, like  
the method did ? When is this useful ?

Thanks

---

<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, 5:40pm UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197/7 "2006-04-20T17:40:09Z")

</div>

> Well, I’ve not understood but if you want to know why Method#to\_proc  
> exist  
> this is perhaps to give you this possibility
> 
> class A  
> def initialize(x)  
> @x = x  
> end
> 
> ```
> def a
> p self
> end
> 
> def b
> yield
> end
> 
> ```
> 
> end
> 
> a = A.new(1)  
> A.new(2).b &a.method(:a)

Can you please explain how this works ?

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 20, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197/8 "2006-04-20T14:15:42Z")

</div>

> > > > > “E” == Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

E\> I’m not questioning your design at all - I ask out of sheer curiosity  
E\> what this language construct (Proc bound to an object) does, because  
it  
E\> isn’t documented anywhere (please correct me if I’m mistaken).  
Moreover,  
E\> I’ll be happy to see an example of when this can be useful in Ruby  
E\> coding.

Well, I’ve not understood but if you want to know why Method#to\_proc  
exist  
this is perhaps to give you this possibility

class A  
def initialize(x)  
@x = x  
end

```
  def a
     p self
  end

  def b
     yield
  end

```

end

a = A.new(1)  
A.new(2).b &a.method(:a)

Now, is this useful or not …

Guy Decoux

---

<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, 5:48pm UTC](https://www.ruby-forum.com/t/understanding-the-behavior-of-method-to-proc/58197/9 "2006-04-20T17:48:36Z")

</div>

> > > > > “E” == Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > A.new(2).b &a.method(:a)

E\> Can you please explain how this works ?

Well `&’ call #to\_proc to create a Proc object (from the Method object  
#a  
in this case) and then the block is given to the method #b.

When yield is called, the block is executed with `self = a’

Guy Decoux
