# How to store a block in a var?

**URL:** <https://www.ruby-forum.com/t/how-to-store-a-block-in-a-var/124685>\
**Category:** Ruby\
**Created:** [January 1, 2008, 3:59am UTC](https://www.ruby-forum.com/t/how-to-store-a-block-in-a-var/124685 "2008-01-01T03:59:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [January 1, 2008, 3:59am UTC](https://www.ruby-forum.com/t/how-to-store-a-block-in-a-var/124685/1 "2008-01-01T03:59:41Z")

</div>

Mil perdones, but my brain seems to have gone awol. I have multiple  
places in a spec where I need to run the same block of code. I would  
be much happier storing this block in a var as a Proc as opposed to  
wrapping it up in a method. Is there a way I can do this? My spec  
looks like this:

do\_stuff do  
stuff  
end

where “stuff” is the part I want to wrap up into some tidy thing. I  
tried it with lambda but my brain was too tired.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [January 1, 2008, 4:03am UTC](https://www.ruby-forum.com/t/how-to-store-a-block-in-a-var/124685/2 "2008-01-01T04:03:11Z")

</div>

Giles B. wrote:

> where “stuff” is the part I want to wrap up into some tidy thing. I  
> tried it with lambda but my brain was too tired.

var = Proc.new { stuff }

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [January 1, 2008, 4:07am UTC](https://www.ruby-forum.com/t/how-to-store-a-block-in-a-var/124685/3 "2008-01-01T04:07:17Z")

</div>

> var = Proc.new { stuff }

yah, but can I just pass it as a block of code?

do\_stuff do  
var.call  
end

??

the thing is that do\_stuff is the only thing that makes the code in  
var meaningful, so when I do that now, I’m getting NoMethodError: no  
method “call” for nil

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [January 1, 2008, 7:59am UTC](https://www.ruby-forum.com/t/how-to-store-a-block-in-a-var/124685/4 "2008-01-01T07:59:42Z")

</div>

On Dec 31, 2007, at 10:06 PM, Giles B. wrote:

> > var = Proc.new { stuff }
> 
> yah, but can I just pass it as a block of code?
> 
> do\_stuff do  
> var.call  
> end

do\_stuff(&var)

Gary W.
