Procs and Lambdas
Procs and lambdas are saved blocks. Lambdas check arguments and return from themselves.
Example
sq = ->(x) { x * x }
add = lambda { |a, b| a + b }
pr = proc { |x| (x || 0) + 1 }
puts sq.call(5), add.(1, 2), sq[3], pr.call
puts [1, 2, 3].map(&sq).inspectResult
25 3 9 1 [1, 4, 9]
Exercise
Stabby lambda.
sq = (x) { x * x }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.