ruby - 通过方法调用时如何将参数传递给proc?

标签 ruby parameters call block proc-object

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/

相关文章:

ruby-on-rails - #<Class :XYZ> (NoMethodError) [rspec] 的未定义方法 'visit'

arrays - 奇怪的行为:输入参数(具有以 “D”或 “L”字符结尾的多个值)

c++ - Microsoft Detour - 带有汇编程序 "call"指令的 Hook 函数

javascript - 调用 javascript 函数太多次

ruby - 如何向将显示在 ruby​​gems.org 上的 gem 添加文档?

ruby - [\s\S]* 有什么区别?和 。*?

javascript - 如何使用 javascript 将数据存储为 url 参数?

C#:如何在我的程序中添加对 "/?"的响应

运行其他模块的VBA模块

ruby-on-rails - 如何使用语义 UI 设置 Rails 应用程序?