Project

object-let

0.0
No commit activity in last 3 years
No release in over 3 years
Defines Object#let, which yields the object and returns the result
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

object-let

Gem Version Build Status Dependency Status

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, or at the tail of a method chain.

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, this idiom can make clear that the intermediate result is of no importance outside the block.

You can also think of this as analogous to "map" in a method chain, but for a single value rather than for an enumerable. Compare:

array_of_items = thingy.item_names.map { |name| Item.new(:name => name) }
just_one_item  = thingy.item_name.let  { |name| Item.new(:name => name) }

See also

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

The "let" gem (https://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"

Compatibility

The gem is tested on ruby 2.1.9, 2.2.5, and 2.3.1

History

  • 1.0.0 - remove Object#let_if; on ruby >= 2.3 using &.let is cleaner and nearly equivalent (differ only in falsey vs nil). Drop support for ruby <= 1.9

  • 0.1.0 - Add Object#let_if

  • 0.0.1 - Initial version

Copyright

Released under the MIT License. See LICENSE for details.