I can pass multiple parameters and at last one block parameter to method. But it shows error when i try to pass more than one block. I want to know how it can be done?
def abc(x, &a)
x.times { a.call("hello") }
end
abc(3) {|a| puts "#{a} Sana"}
abc(1, &proc{|a| puts "#{a} Sana"})
But below definition gives error
def xyz(x, &a, &b)
puts x
a.call
b.call
end
You can use Proc
:
def xyz(x, a, &b)
puts x
a.call
b.call
end
xyz(3, Proc.new { puts 'foo' }) { puts 'bar' }
# or simpler
xyz(3, proc { puts 'foo' }) { puts 'bar' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With