Используйте Proc.new
для захвата блока, например
def callBlocks(*args, &block)
args.each { |arg| arg.call }
block.call if block_given?
end
my_proc1 = Proc.new { puts "proc1" }
my_proc2 = Proc.new { puts "proc2" }
callBlocks(my_proc1, my_proc1, my_proc2) {
puts "block"
}
производит:
proc1
proc1
proc2
block
Хотите поспорить с этим?
def callBlocks(*args, &block)
args.each { |arg| arg.call(1, 2, 3) }
block.call(4, 5, 6) if block_given?
end
my_proc1 = Proc.new { |a, b, c| puts "proc1 with #{a}, #{b}, #{c}" }
my_proc2 = Proc.new { |a, *dummy| puts "proc2 only needs #{a}" }
callBlocks(my_proc1, my_proc1, my_proc2) { |*abc|
puts "block with #{abc.join('/')}"
}
производит:
proc1 with 1, 2, 3
proc1 with 1, 2, 3
proc2 only needs 1
block with 4/5/6