What are these Lambdas you speak of?
This article is focused on Lambdas as used in the Ruby language.
What are Lambdas? They’re given several names in other languages.
- Lambda
- Anonymous Function
- Closure
In Ruby, we just call it a Lambda function. It’s defined as such:
x = lambda { return "ar har har har" }
Calling this method will return “ar har har har” as a return value. If that were put into a function, like so,
def foo x = lambda { return "ar har har har" } x.call return "yo ho ho ho" end
This will actually return “yo ho ho ho”. If you were to puts x.call, however, you would see “ar har har har”. Very interesting. So returning from lambda acts just as a function would, hence it’s an anonymous function.
Lambdas have an interesting quirk in that if you declare one as such:
x = lambda { |x, y| puts x + y }
And then call it like this:
x.call(1, 2, 3)
It will throw an argument error


