passing a block around...

J

J2M

I want to pass a block to method foo then call method bar from foo and
have bar yield to the block, I also want to be able to call bar
directly with a block, how to do this neatly.

e.g. I want to be able to do

foo { |baz| do stuff with baz }
bar { |baz| do other stuff with baz }

but when foo gets a block it passes it to bar in the background.

Thanks,
James
 
D

dblack

Hi --

Well, while I don't quite understand everything you've said here, the
mechanism for passing blocks around is simple:

def foo(&block)
baz block

You need &block rather than just block.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top