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 ?
謝謝:)如何通過方法調用參數時將參數傳遞給proc? (紅寶石)
您應該在每行代碼前加上2個空格,以使其成爲答案中的代碼示例。它看起來更漂亮,併爲所有代碼行添加語法高亮顯示。 – David 2010-08-04 15:50:43
不需要',&block' – 2010-08-04 16:55:51
@ Marc-AndréLafortune:你指的是'thank'的定義,而不是它的調用,對吧? – 2010-08-04 23:31:28