Skip to content
Defines `Kernel#its` and `Kernel#it` that queue and defer method calls, simplifying list comprehension and case statements.
Pull request Compare This branch is 40 commits ahead, 6 commits behind jicksta:master.
Latest commit 759bc05 Feb 22, 2016 @ronen version bump 1.2.1
Failed to load latest commit information.



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 an array. E.g.

{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.0.0 and 2.2.3

(MRI ruby 1.8.7 was supported up through version 1.1.1)


Release Notes

  • 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 updated to be compatible with ruby 1.9 and gemspec, added case statement support, renamed its-it, and installed on Unlike methodphitamine, this gem includes only its and it, not the "maybe" monad.

Something went wrong with that request. Please try again.