[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to store a block in a var?

Giles Bowkett

1/1/2008 2:59:00 AM

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 Bowkett

Podcast: http://hollywoodgrit.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

3 Answers

Tim Hunter

1/1/2008 3:02:00 AM

0

Giles Bowkett wrote:
> 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.
>

var = Proc.new { stuff }

--
RMagick: http://rmagick.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

Giles Bowkett

1/1/2008 3:07:00 AM

0

> 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 Bowkett

Podcast: http://hollywoodgrit.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

Gary Wright

1/1/2008 6:59:00 AM

0


On Dec 31, 2007, at 10:06 PM, Giles Bowkett 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 Wright