object-let

Defines Object#let, which simply yields the object and returns the result. This idiom, familiar to Lisp programmers, can be handy to eliminate the need for an intermediate variable when you need to use the result of a computation multiple times.

For example, without let, you might write:

biggest = my_things.find_biggest
bounds = Bound.new(:width => biggest.width, :height => biggest.height)

with let, this could be:

bounds = my_things.find_biggest.let { |biggest|
  Bound.new(:width => biggest.width, :height => biggest.height)
}

Stylistically, as well in terms of lexical scoping (for ruby 1.9), this idiom can make clear that the intermediate result is of no importance outside the block.

See also

Discussion at www.opensourcery.com/blog/zack-hobson/objectlet-ruby-0

Alternative implementation at ick.rubyforge.org/inside.html

The “let” gem (rubygems.org/gems/let) provides a module that can be included in a class to define memoizing accessors. That gem and this one are compatible.

Installation

Install via:

% gem install object-let

or in your Gemfile:

gem "object-let"

Released under the MIT License. See LICENSE for details.