0.4 Blocks, Procs, and Lambdas

Lambda vs. Proc

Blocks in Ruby have had a rather complicated history, which means that the terminology gets weird, and there are a small handful of edge-cases that you should be aware of.

I've already mentioned how Proc, Block and Lambda are often used interchangeably in Ruby and this is fine most of the time. Let’s try to understand what each term actually means, and what the differences between them are, if any.

Note: This section only deals with Ruby 1.9.x and does not apply to Ruby 1.8.x.

Example Code:

Output Window

As you can see, both approaches produce an instance of a Proc, though the one created using lambda is clearly distinguished with the word “lambda” in parentheses - an unusual deviation from the norm.

How are these objects different?

A block created with lambda behaves like a method when you use return and simply exits the block, handing control back to the calling method.

A block created with Proc.new behaves like it’s a part of the calling method when return is used within it, and returns from both the block itself as well as the calling method.

Here’s an example that uses lambdas - the return within the block hands control back to the method.

Example Code:

Output Window

Here’s one that uses Proc.new - the return within the block exits not just the block itself, but also the surrounding method.

Example Code:

Output Window

As a consequence, Proc.new is something that’s hardly ever used to explicitly create blocks because of these surprising return semantics. It is recommended that you avoid using this form unless absolutely necessary.

Choices, choices

Let’s summarize all the different ways in which blocks can be created in Ruby that we have learned thus far.

  1. Implicitly when invoking a method
  2. Explicitly using the Kernel#lambda factory method
  3. Explicitly using Proc.new

Both (2) and (3) have alternative syntaxes available to make for terser code. Here they are:

The -> literal form is a shorter version of Kernel#lambda. The following two lines produce identical results.

Example Code:

Hint

short-lambda

Output Window

Kernel#proc factory method is identical to Proc.new. Note that proc is a method and not a literal form like -> nor a keyword like yield.

The following two lines produce identical results.

Example Code:

Output Window

All done!

Ok, that’s all for blocks. You may wish to re-visit material on Array enumeration, one of the most common instances of the use of blocks in Ruby.

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2014 by Jasim A Basheer