【发布时间】:2009-09-16 18:19:22
【问题描述】:
s = Proc.new {|x|x*2}
puts s.call(5)
-> 10
def foo(&a)
a.call(5)
end
puts "test foo:"
foo(s)
当我尝试调用上面的过程时,我得到:
foo: wrong number of arguments (1 for 0) (ArgumentError)
我的期望是,如果方法是用这种类型的签名定义的,我可以将 proc 传递给方法:
def foo(&a)
然后我可以像这样执行 proc insiide foo:
a.call(5)
【问题讨论】:
-
使用 "def foo(&a)" 你也可以 "yield(5)" 到块。
标签: ruby proc-object