As an
addendum to the previous
examples, the way to
explicitly create a
closure in
Ruby is to use the
syntax
closure = lambda {|x| x + x}
or
closure = Proc.new {|x| x + x}
For example,
lambda {|x| x + x}.call 2 would return 4
Of course, the neatest part about it is that there's nothing special about the lamba
function that makes it necessary to create closures with! You could even create your own closure-creation
method, like so:
def my_lambda(&block)
raise ArgumentError, "tried to create Procedure-Object without a block" if !block_given?
block
end