rui
rui

Reputation: 11284

Is it possible to pass more than one block to a method in Ruby?

Something like:

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

foo() { puts "one" } { puts "two" }

Upvotes: 10

Views: 1072

Answers (2)

horseyguy
horseyguy

Reputation: 29895

syntax is a lot cuter in Ruby 1.9:

foo ->{puts :one}, ->{puts :two}

Upvotes: 3

Mark Rushakoff
Mark Rushakoff

Reputation: 258148

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>

Upvotes: 16

Related Questions