我可以将多个参数和最后一个块参数传递给方法.但是当我尝试传递多个块时它会显示错误.我想知道怎么做?
def abc(x, &a)
x.times { a.call("hello") }
end
abc(3) {|a| puts "#{a} Sana"}
abc(1, &proc{|a| puts "#{a} Sana"})
Run Code Online (Sandbox Code Playgroud)
但是低于定义会给出错误
def xyz(x, &a, &b)
puts x
a.call
b.call
end
Run Code Online (Sandbox Code Playgroud)
Son*_*tos 12
你可以使用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' }
Run Code Online (Sandbox Code Playgroud)