我知道应该使用Procs和lambdas的不同情况(lambda检查参数数量等),但是它们会占用不同的内存量吗?如果是这样,哪一个更有效率? Lambdas和Procs之间存在一些差异. Lambdas拥有所谓的“小额回
> Lambdas拥有所谓的“小额回报”.这意味着Lambda会将流返回到调用它的函数,而Proc将返回调用它的函数.
def proc_demo
Proc.new { return "return value from Proc" }.call
"return value from method"
end
def lambda_demo
lambda { return "return value from lambda" }.call
"return value from method"
end
proc_demo #=> "return value from Proc"
lambda_demo #=> "return value from lambda"
> Lambdas检查传递给它们的参数数量,而Procs则没有.例如:
lambda { |a, b| [a, b] }.call(:foo)
#=> #<ArgumentError: wrong number of arguments (1 for 2)>
Proc.new { |a, b| [a, b] }.call(:foo)
#=> [:foo, nil]
