是否有任何"简单"的解释过程和lambda在红宝石?

有帮助吗?

解决方案

Lambda(其存在其他语言)是象特设职能,创造了只为一个简单的使用,而不是执行一些复杂的行动。

当您用一样的方法 Array#collect 这需要一块 {}, ,你在本质上创造一个氧/proc/框只有使用这一方法。

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

看看 匿名的功能 在维基百科,和一些 其他这样的问题 对细微差别的 lambdaProc.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top