Are there any “simple” explanations for what happens in rubies and lambda?

Are there any “simple” explanations of what procs and lambdas are in Ruby?

+5
source share
1 answer

Lambdas (which exist in other languages) are similar to special functions created just for simple use, and not for performing some complex actions.

When you use a method of a type Array#collectthat takes a block in {}, you essentially create a lambda / proc / block just to use this method.

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

SO lambda Proc.

+5

All Articles