What are blocks, procs, and lambdas?
Coder Talk: Examples of closures in ruby.
Plain old english: Ways of grouping code we want to run.
While it looks like these are all very similar, there are subtle differences that I will cover below.
Differences between Blocks and Procs
1. Procs are objects, blocks are not
A proc (notice the lowercase p) is an instance of the Proc class.
This lets us call methods on it and assign it to variables. Procs can also return themselves.
In contrast, a block is just part of the *syntax* of a method call. It doesn’t mean anything on a standalone basis and can only appear in argument lists.
2. At most one block can appear in an argument list
In contrast, you can pass multiple procs to methods.
Differences between Procs and Lambdas
Before I get into the differences between procs and lambdas, it is important to mention that they are both Proc objects.
However, lambdas are a different ‘flavor’ of procs. This slight difference is shown when returning the objects.
The (lambda) notation is a reminder that while procs and blocks are very similar, even both instances of the Proc class, they are also slightly different. Below are the key differences.
1. Lambdas check the number of arguments, while procs do not
In contrast, procs don’t care if they are passed the wrong number of arguments.
As shown above, procs don’t freak out and raise errors if they are passed the wrong number of arguments. If the proc requires an argument but no argument is passed then the proc returns nil. If too many arguments are passed than it ignores the extra arguments.
2. Lambas and procs treat the ‘return’ keyword differently
‘return’ inside of a lambda triggers the code right outside of the lambda code
‘return’ inside of a proc triggers the code outside of the method where the proc is being executed
So what is a closure?
Coder Talk: ‘A function or a reference to a function together with a referencing environment. Unlike a plain function, closures allow a function to access non-local variables even when invoked outside of its immediate lexical scope.’ – Wikipedia
Plain old english: Similar to a suitcase, it’s a group of code that when opened (i.e. called), contains whatever was in it when you packed it (i.e. created it).
Background Part 1: Lambda Calculus and Anonymous Functions
Lambda get its name from a type of calculus introduced in the 1930s to help investigate the foundations of mathematics. Lambda calculus helps make computable functions easier to study by simplifying its semantics. The most relevant of these simplifications is that is treats functions ‘anonymously’, meaning that no explicit names are given to functions.
Background Part 2: Where Does the Name Proc Come From
Proc is short for procedure, which is a set of instructions packaged as a unit to perform a specific task. In different languages these may be called functions, routines, methods, or the generic term callable units. They are usually made to be called multiple times and from multiple times in a program.
- Procs are objects, blocks are not
- At most one block can appear in an argument list
- Lambdas check the number of arguments, while procs do not
- Lambdas and procs treat the ‘return’ keyword differently
Make yourself useful.