Zub*_*air 5 ruby lambda proc-object
对于Ruby中的procs和lambdas有什么"简单"的解释吗?
Lambdas(也存在于其他语言中)就像ad hoc函数,仅为简单使用而不是执行某些复杂操作而创建.
当你使用像Array#collect这样的方法时{},你实际上只是使用该方法创建一个lambda/proc/block.
a = [1, 2, 3, 4]
# Using a proc that returns its argument squared
# Array#collect runs the block for each item in the array.
a.collect {|n| n**2 } # => [1, 4, 9, 16]
sq = lambda {|n| n**2 } # Storing the lambda to use it later...
sq.call 4 # => 16
Run Code Online (Sandbox Code Playgroud)
见匿名函数在维基百科上,还有一些其他的做题的细微差别lambda对比Proc.