10

Something like:

def foo(&b1, &b2)
  b1.call
  b2.call
end

foo() { puts "one" } { puts "two" }
rui
  • 11,015
  • 7
  • 46
  • 64
  • possible duplicate of [Passing multiple codeblocks as arguments](http://stackoverflow.com/questions/2463612/passing-multiple-codeblocks-as-arguments) – Arturo Herrero Jan 24 '15 at 21:29

2 Answers2

16

You can't pass multiple blocks in that way, but you can pass multiple proc or lambda objects:

irb(main):005:0> def foo(b1, b2)
irb(main):006:1>   b1.call
irb(main):007:1>   b2.call
irb(main):008:1> end
=> nil
irb(main):009:0> foo(Proc.new {puts 'one'}, Proc.new {puts 'two'})
one
two
=> nil
irb(main):010:0>
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 3
    Ok I see. I could also use lambda { puts "bla" } I suppose. I guess the bottom line is that I can only use &block at most once in a method definition. Thanks. – rui Aug 24 '10 at 16:57
3

syntax is a lot cuter in Ruby 1.9:

foo ->{puts :one}, ->{puts :two}
horseyguy
  • 29,455
  • 20
  • 103
  • 145
  • 1
    Note that this replaces `foo(Proc.new {puts 'one'}, Proc.new {puts 'two'})` in [Mark's answer](https://stackoverflow.com/a/3558899), you still need to define the function as `foo(b1,b2)`, not `foo(&b1, &b2)` – jrh Feb 26 '21 at 20:01