Skip to content

ronen/object-let

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Ruby gem defines Object#let

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages