In Ruby, lambdas are anonymous function code blocks that can take zero or more arguments. They can then be stored or passed in other values and called primarily with the #call method.


If zero arguments are used, use either -> or lambda:

myLambda = lambda { puts "Hello, World!" }
puts # Output: Hello, World!

If 1 or more arguments are used, use ->:

myLambda = -> (v) { v * 2 }
puts # Output: 4
Edit this page on GitHub


Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn Ruby on Codecademy