# Proc to block

**URL:** <https://www.ruby-forum.com/t/proc-to-block/125313>\
**Category:** Ruby\
**Created:** [January 8, 2008, 5:08am UTC](https://www.ruby-forum.com/t/proc-to-block/125313 "2008-01-08T05:08:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![L\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ba0ec/32.png) [@L\_A](https://www.ruby-forum.com/u/L_A)\
**Post date:** [January 8, 2008, 5:08am UTC](https://www.ruby-forum.com/t/proc-to-block/125313/1 "2008-01-08T05:08:24Z")

</div>

Hello all,

Is there a way to turn a Proc into a block for the purposes of methods  
that yield to blocks? It’s not a necessity as I can always wrap the  
Proc in a block, I’m just curious.

Thanks,  
Loren

```
  ____________________________________________________________________________________

```

Never miss a thing. Make Yahoo your home page.  
[http://www.yahoo.com/r/hs](http://www.yahoo.com/r/hs)

---

<div class="post-metadata">

**Author:** ![L\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ba0ec/32.png) [@L\_A](https://www.ruby-forum.com/u/L_A)\
**Post date:** [January 8, 2008, 5:35am UTC](https://www.ruby-forum.com/t/proc-to-block/125313/2 "2008-01-08T05:35:51Z")

</div>

On Jan 7, 2008, at 11:06 PM, L A wrote:

> Hello all,
> 
> Is there a way to turn a Proc into a block for the purposes of  
> methods that yield to blocks? It’s not a necessity as I can always  
> wrap the Proc in a block, I’m just curious.

Just prefix the object you want to be passed as a block with ‘&’ in  
the argument list.

p = proc { |x| x \> 10 }

[1,5,10,20, 30].select &p

Gary W.

---

<div class="post-metadata">

**Author:** ![L\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ba0ec/32.png) [@L\_A](https://www.ruby-forum.com/u/L_A)\
**Post date:** [January 8, 2008, 5:37am UTC](https://www.ruby-forum.com/t/proc-to-block/125313/3 "2008-01-08T05:37:37Z")

</div>

On Jan 8, 2008 12:06 PM, L A [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to turn a Proc into a block for the purposes of methods that yield to blocks? It’s not a necessity as I can always wrap the Proc in a block, I’m just curious.

maybe use the & op, like

p=proc{“hi, i’m proc”}  
#=\> #Proc:0x028cc40c@:1(irb)  
p.call  
#=\> “hi, i’m proc”  
def q  
yield  
end  
#=\> nil  
q  
LocalJumpError: no block given  
from (irb):7:in `q’  
from (irb):9  
from :0  
q &p  
#=\> “hi, i’m proc”  
(proc &p).call  
#=\> “hi, i’m proc”

i am not sure why you want to wrap a proc with a block though

proc {p}  
#=\> #Proc:0x02898418@:30(irb)  
(proc {p}).call  
#=\> #Proc:0x028cc40c@:1(irb)  
(proc {p}).call.call  
#=\> “hi, i’m proc”

kind regards -botp

---

<div class="post-metadata">

**Author:** ![L\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ba0ec/32.png) [@L\_A](https://www.ruby-forum.com/u/L_A)\
**Post date:** [January 8, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/proc-to-block/125313/4 "2008-01-08T15:06:07Z")

</div>

On Jan 8, 8:24 am, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > wrap the Proc in a block, I’m just curious.

Wait… what?

No it doesn’t.

[1, 5, 10, 20, 30].map &:to\_a

uses Symbol#to\_proc

p = proc { |x| x \> 10 }  
[1,5,10,20, 30].select &p

is standard ruby syntax, even from 1.6:

“If the last argument to a method is preceded by an ampersand, Ruby  
assumes that it is a Proc object. It removes it from the parameter  
list, converts the Proc object into a block, and associates it with  
the method.”  
(see  
[Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html#UD))

---

<div class="post-metadata">

**Author:** ![L\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ba0ec/32.png) [@L\_A](https://www.ruby-forum.com/u/L_A)\
**Post date:** [January 8, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/proc-to-block/125313/5 "2008-01-08T16:40:10Z")

</div>

On Jan 8, 2008 9:05 AM, Noah E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > relies on having a Symbol#to\_proc method  
> > [1,5,10,20, 30].select &p
> 
> is standard ruby syntax, even from 1.6:
> 
> “If the last argument to a method is preceded by an ampersand, Ruby  
> assumes that it is a Proc object. It removes it from the parameter  
> list, converts the Proc object into a block, and associates it with  
> the method.”

This isn’t what I said.

Gary suggested using a symbol as a proc, which requires that Symbol  
implement the to\_proc method, which is NOT part of standard Ruby prior  
to 1.9.

$ qri to\_proc  
------------------------------------------------------ Multiple choices:

```
 Method#to_proc, Proc#to_proc, Test::Unit::Util::ProcWrapper#to_proc

```

Now as I pointed out extending Symbol to implement to\_proc is quite  
common, and many Rails programmers tend to think of the stuff in  
ActiveSupport as being standard Ruby, but it isn’t.

It’s likely that Rails will change ActiveSupport to only add  
Symbol#to\_proc if using Ruby \< 1.9 since 1.9 includes it, but that  
doesn’t seem to have happened yet.  
[http://dev.rubyonrails.org/ticket/8818](http://dev.rubyonrails.org/ticket/8818)

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![L\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ba0ec/32.png) [@L\_A](https://www.ruby-forum.com/u/L_A)\
**Post date:** [January 8, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/proc-to-block/125313/6 "2008-01-08T14:26:06Z")

</div>

On Jan 7, 2008 11:35 PM, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the argument list.
> 
> p = proc { |x| x \> 10 }
> 
> [1,5,10,20, 30].select &p

Except that this isn’t part of the standard ruby library for 1.8. It  
relies on having a Symbol#to\_proc method

It is a common extension, for example Rails includes it.

It IS part of Ruby 1.9 though.

I’d suggest googling for “ruby to\_proc”

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![L\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ba0ec/32.png) [@L\_A](https://www.ruby-forum.com/u/L_A)\
**Post date:** [January 8, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/proc-to-block/125313/7 "2008-01-08T17:33:28Z")

</div>

On Jan 8, 2008, at 10:39 AM, Rick DeNatale wrote:

> Gary suggested using a symbol as a proc, which requires that Symbol  
> implement the to\_proc method, which is NOT part of standard Ruby prior  
> to 1.9.

Rick, you just misread my original post a bit. I didn’t mention  
Symbol#to\_proc at all but I did hint at it by saying you ‘just’  
prefix the argument with ‘&’. That sort of begs the question of  
what happens if the argument isn’t an instance of Proc but I didn’t  
want to muddy the waters at that point. My example used a proc.

Gary W.

---

<div class="post-metadata">

**Author:** ![L\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ba0ec/32.png) [@L\_A](https://www.ruby-forum.com/u/L_A)\
**Post date:** [January 8, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/proc-to-block/125313/8 "2008-01-08T16:57:08Z")

</div>

Rick DeNatale wrote:

> Gary suggested using a symbol as a proc

No, he didn’t.
