proc = Proc.new do |name|
puts "Thank you #{name}!"
end
def thank
yield
end
proc.call # output nothing, just fine
proc.call('God') # => Thank you God!
thank &proc # output nothing, too. Fine;
thank &proc('God') # Error!
thank &proc.call('God') # Error!
thank proc.call('God') # Error!
# So, what should I do if I have to pass the 'God' to the proc and use the 'thank' method at the same time ?
谢谢:)
最佳答案
我认为最好的办法是:
def thank name
yield name if block_given?
end
关于ruby - 通过方法调用时如何将参数传递给proc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3406969/