gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 06:51:40 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com