Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
149 lines (101 sloc) 4.03 KB



This gem defines kernel methods its and it that queue and defer method calls. This is handy for list comprehension and case statements.

Gem Version Build Status Dependency Status

List Comprehension

its and it extend the Symbol#to_proc idiom to support chaining multiple methods.

When performing a list comprehension ruby, you can use a block argument:{ |user| }

Or, to avoid needing the block and and extra parameter, you can use the Symbol#to_proc shortcut: &:contact

But if you want to chain several methods, such as:{ |user| }

The Symbol#to_proc shortcut doesn't help much. At best, if you're willing to accept intermediate arrays, you can do:

To improve the situation, this gem provides a Kernel method its, which lets you get the same shortcut advantages as Symbol#to_proc but supports chaining: &

Also, its supports arguments and blocks, allowing you to do things like &[0,3].capitalize & > 10 { |address| airline.flies_to })

As a syntactic sugar, it is an alias for its, to use with methods that describe actions rather than posessives. For example: &it.to_s.capitalize

Hash comprehensions

When used with hash comprehensions, the |key, val| pair of arguments are presented to its as a tuple that can be accessed array-like via [0] or [1] and/or struct-like via #key and #value methods. E.g.

{dogs: 1, cats: 2, goats:3}.select &its.key =~ /^c/ # => {cats: 2}
{dogs: 1, cats: 2, goats:3}.select &its.value.even? # => {cats: 2}
{dogs: 1, cats: 2, goats:3}.select &its[1].even? # => {cats: 2}

Case statements

its and it similarly extend Ruby's case mechanism to support testing arbitrary methods, minimizing the need to create temporary variables. That is, instead of:

maxlen =
when maxlen > 10000 then "too big"
when maxlen < 10    then "too small"
else                     "okay"

You can use it:

when it > 1000 then "too big"
when it < 10   then "too small"
else                "okay"

Of course method chanining can be used here too:

case users.first
when == "Gimme Cookie" then ...
when > 10 then ...
else ...

Under the hood

The it method creates an instance of the ItsIt::It class, which uses method_missing to capture and queue up all methods and their arguments except for :to_proc and :=== (and also excepting :respond_to? :to_proc and :respond_to? :===).

:to_proc returns a proc that will evaluate the method queue on a given argument. :=== takes an argument and evaluates that proc, returning the result.


Install as usual from via

$ gem install "its-it"

or in a Gemfile

gem "its-it"


Tested on MRI ruby 1.9.3, 2.1.9, 2.2.5, and 2.3.1

(MRI ruby 1.8.7 was supported up through version 1.1.1)


Release Notes

  • 1.3.0 Add #key and #value for Hash comprehensions; plus minor internal cleanup.
  • 1.2.1 Don't leak all of ItsIt into main, just ItsIt::Kernel. Thanks to klg
  • 1.2.0 Add support for Hash comprehensions; drop support for ruby 1.8.7
  • 1.1.1 Remove dependency on BlankSlate

This gem is orignally based on Jay Philips' methodphitamine gem. It has been made available on, and updated over the years.