What has been in the works lately? It'll blow your mind.

Generic placeholder image

This stuff is pretty amazing

Blocs, Procs, Lambdas

Blocs

Ruby blocks are passed to iterator methods such as each. The method call is a syntax which accepts a block. Methods such as each accepting this block is considered to be a syntax type of coding rather than the method getting passed in as an argument because the block is not an argument or an object.

Procs

What if we wanted to actually name a chunk of code as an object and assign it to a variable so we could pass it to a method as an argument? Then we would use a ruby Proc. An object instantiated from the class Proc is a ruby object which represents a chunk of code. The object can be assigned to a variable and passed to a method as an argument.

Lambdas

Lambdas are a variation on the Proc object, but has several differences in characteristics. Lambdas are also chunks of code, but Lambdas require explicit creation and can't be called by &block, with the & sign. lambdas return to the code block in which they are defined, therefore a return expression inside the method code in which they defined will also occur. A Proc on the other hand will return a value for the code block in which the method is defined. If the return value of the Proc takes place then other return expressions of the method will not execute.

The final differences is that lambdas will not execute and raise an argument if it does not get passed the correct number of arguments. A proc will run with arguments defaulted to nil if not specified the correct number of arguments.