passing a block around...

Discussion in 'Ruby' started by J2M, Dec 31, 2006.

  1. J2M

    J2M Guest

    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
    J2M, Dec 31, 2006
    #1
    1. Advertising

  2. J2M

    Guest

    Hi --

    On Mon, 1 Jan 2007, Jason Roelofs wrote:

    >
    > On 12/31/06, J2M <> wrote:
    >>
    >> 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.
    >>

    > 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)
    , Dec 31, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven
    Replies:
    9
    Views:
    2,994
    Steven
    Sep 24, 2003
  2. Piet
    Replies:
    0
    Views:
    506
  3. morrell
    Replies:
    1
    Views:
    933
    roy axenov
    Oct 10, 2006
  4. Network/Software Buyer
    Replies:
    0
    Views:
    402
    Network/Software Buyer
    May 23, 2010
  5. Brian Candler

    Passing a block to a block

    Brian Candler, Nov 4, 2008, in forum: Ruby
    Replies:
    2
    Views:
    101
    Brian Candler
    Nov 4, 2008
Loading...

Share This Page