A function closure is a function or reference to a function, together with a referencing environment - a table storing a reference to each of the non-local variables even when invoked outside its immediate lexical scope. ("Closure (computer programming)").
Blocks, Procs, and Lambdas are special methods within Ruby. Since Ruby is an Object Orientated Language (OOL), that means most everything is an object...almost everything. The topic for this blog is Blocks, Procs, and Lambdas; below I will break down each show how to use them and some background information about them.
Blocks define an instruction between the curly braces, or between the "do"... "end", which may optionally take an argument and return some values. Basically all that means is a Block is just a bit of code between "do"..."end" or {}. It's not an object on its own, but it can be passed to methods like .each or .map, .zip etc.
1.times do puts "This is a code block!" end
or
1.times {puts "This is another code block!"}
Proc (Procedures) objects are blocks of code that have been bound to a set of local variables. Once bond, the code may be called in different contexts and still access those variables. The difference between a Block and a Proc, Procs are full-fledged objects. So they have all the power and abilities of objects (Blocks do not.). Also unlike blocks, procs can be called over and over without rewriting them. This prevents you from having to retype the contents of your block every time you need to execute a particular bit of code.
Procs are easy to define! You just call Proc.new and pass in the block you want to save.
Cube=Proc.new {|x| x**3}
We can then pass the Procs to a method that would otherwise take a block.
[1,2,3].collect!(&cube)
The "&" is used to convert the cube proc into a block. We'll do this any time we need to pass a proc to a method that expects a block. Lambda is just like a Procs, only it cares about the number of arguments it gets and it returns to its calling method rather than returning immediately. Like Procs, Lambdas are objects, the similarities between the both don't just stop there (with the exception of a bit of syntax and a few behavioral quirks), and they are pretty much identical . . . but do have some key differences.
The First difference between a Lambda and a Procs, a Lambda checks that a number of arguments passed to it, while a Procs does not (mentioned above). This means that a lambda will throw an error if you pass it the wrong number of arguments, whereas a Procs will ignore unexpected arguments and assign "nil" to anything that is missing. The second thing is when a lambda returns, it passes control back to the calling method; when a process returns, it does so immediately, without going back to the calling method. What does that mean? Pretty much a Lambda is just a function without a name.
Lambdas are also easy to create like Procs:
Lambda { | param | block }
Example of lambda:
1 = lambda { "Hello DBC" } puts 1.call
Cited Sources:
"Closure (computer programming)" Wikipedia: The Free Encyclopedia. 26 Jun 2014. Source