# Passing procs around in variables

**URL:** <https://www.ruby-forum.com/t/passing-procs-around-in-variables/131428>\
**Category:** Ruby\
**Created:** [March 9, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/passing-procs-around-in-variables/131428 "2008-03-09T23:49:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c5a1d2/32.png) [@nathan](https://www.ruby-forum.com/u/nathan)\
**Post date:** [March 9, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/passing-procs-around-in-variables/131428/1 "2008-03-09T23:49:11Z")

</div>

Hi,  
This might be something really simple, but I’m trying to write a  
console-based menu for my program. I’ve done this before in a  
procedural language, but I decided to try and port what I’ve done to  
ruby, because it’ll make other parts of my program far easier. My  
program has a few menus, so it’d be nice to have re-usable code (which  
the procedural version didn’t have much of).

The best way I’ve come up with is to pass the method calls which are  
executed by each menu item as procs, but this requires passing them  
around between two objects internally. (I haven’t completely decided  
how I’m going to structure the code which passes the procs… I have  
an idea, but I’ll cross that when I come to it)

Now, the Item class, which each menu item is an object of, doesn’t  
work as expected… take a look here: [Parked at Loopia](http://pastie.caboo.se/163781).  
When I try to call myItem.execute!, it complains “no block given”. If  
I call myItem.execute! { 2 + 1}, then it works, but that’s not what I  
want…

I’ve also included the Menu class, just in case anybody has any advice  
as to how to do the whole thing better.

Thanks,  
-Nathan

---

<div class="post-metadata">

**Author:** ![nathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c5a1d2/32.png) [@nathan](https://www.ruby-forum.com/u/nathan)\
**Post date:** [March 10, 2008, 12:03am UTC](https://www.ruby-forum.com/t/passing-procs-around-in-variables/131428/2 "2008-03-10T00:03:19Z")

</div>

2008/3/9, Nathan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> […]  
> Now, the Item class, which each menu item is an object of, doesn’t  
> work as expected… take a look here: [Parked at Loopia](http://pastie.caboo.se/163781).  
> When I try to call myItem.execute!, it complains “no block given”. If  
> I call myItem.execute! { 2 + 1}, then it works, but that’s not what I  
> want…

> end  
> end

Use yield when the block is _not_ stored in a variable.  
What your yield line does is this: Call the block given to  
execute! with @proc as argument.

Once you have a proc object, you can call it, well, with  
the “call” method. Just change “yield @proc” to  
“@proc.call” and it should work.

HTH,  
Stefan

---

<div class="post-metadata">

**Author:** ![nathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c5a1d2/32.png) [@nathan](https://www.ruby-forum.com/u/nathan)\
**Post date:** [March 10, 2008, 1:48am UTC](https://www.ruby-forum.com/t/passing-procs-around-in-variables/131428/3 "2008-03-10T01:48:23Z")

</div>

On Sun, 09 Mar 2008 17:48:36 -0500, Nathan wrote:

> around between two objects internally. (I haven’t completely decided how
> 
> Thanks,  
> -Nathan

In addition to what Stefan L. said about @proc.call, I just wanted to  
point out that there’s no need to keep different classes in different  
files. (This isn’t Java.)

–Ken

---

<div class="post-metadata">

**Author:** ![nathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c5a1d2/32.png) [@nathan](https://www.ruby-forum.com/u/nathan)\
**Post date:** [March 10, 2008, 12:22am UTC](https://www.ruby-forum.com/t/passing-procs-around-in-variables/131428/4 "2008-03-10T00:22:47Z")

</div>

Thanks, that’s great.

On Mar 9, 11:02 pm, “Stefan L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![nathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c5a1d2/32.png) [@nathan](https://www.ruby-forum.com/u/nathan)\
**Post date:** [March 11, 2008, 3:27am UTC](https://www.ruby-forum.com/t/passing-procs-around-in-variables/131428/5 "2008-03-11T03:27:01Z")

</div>

Yeah, I know. I´m just doing it like that for now, then I´ll put them  
all together at the end. Thanks though.  
-N
