Build Status


Project: github


A Ruby Monad Library, Inspired by Scala

Are you working in both the Scala and Ruby worlds, and finding that you miss some of the practical benefits of Scala's monads in Ruby? Then Rumonade is for you.

The goal of this library is to make the most common and useful Scala monadic idioms available in Ruby via the following classes:

Syntactic support for scala-like for-comprehensions will be implemented as a sequence of calls to #flat_map, #select, etc, modelling Scala's approach.

Support for an all_catch idiom will be implemented to turn blocks which might throw exceptions into Option or Either results. If this proves useful (and a good fit for Ruby), then more narrow functional catchers can be implemented as well.


Option: handle possibly nil values in a functional fashion:

def format_date_in_march(time_or_date_or_nil)
  Option(time_or_date_or_nil).      # wraps possibly-nil value in an Option monad (Some or None)
      map(&:to_date).               # transforms a contained Time value into a Date value
      select {|d| d.month == 3}.    # filters out non-matching Date values (Some becomes None)
      map(&:to_s).                  # transforms a contained Date value into a String value
      map {|s| s.gsub('-', '')}.    # transforms a contained String value by removing '-'
      get_or_else("not in march!")  # returns the contained value, or the alternative if None

format_date_in_march(nil)                            # => "not in march!"
format_date_in_march(Time.parse('2009-01-01 01:02')) # => "not in march!"
format_date_in_march(Time.parse('2011-03-21 12:34')) # => "20110321"


  • each step of the chained computations above are functionally isolated

  • the value can notionally start as nil, or become nil during a computation, without effecting any other chained computations

Either: handle failures (Left) and successes (Right) in a functional fashion:

def find_person(name)
  case name
    when /Jack/i, /John/i
      Left("No such person: #{name.capitalize}")

# success looks like this:
# => Right("Jack")

# failure looks like this:
# => Left("No such person: Jill")

# on the 'happy path', we can functionally combine and transform successes:
(find_person("Jack").lift_to_a + find_person("John").lift_to_a) { |*names| names.join(" and ") }
# => Right("Jack and John")

# while the failure cases are easily handled as well:
(find_person("Jack").lift_to_a +
 find_person("John").lift_to_a +
 find_person("Jill").lift_to_a +
 find_person("Joan").lift_to_a) { |*names| names.join(" and ") }
# => Left("No such person: Jill", "No such person: Joan")

(more examples coming soon…)


There have been many posts and discussions about monads in Ruby, which have sparked a number of approaches.

Rumonade wants to be a practical drop-in Monad solution that will fit well into the Ruby world.

The priorities for Rumonade are:

  1. Practical usability in day-to-day Ruby

    • don't mess up normal idioms of the language (e.g., Hash#map)

    • don't slow down normal idioms of the language (e.g., Array#map)

  2. Rubyish-ness of usage

    • Monad is a mix-in, requiring methods self.unit and #bind be implemented by target classes

    • Prefer blocks to lambda/Procs where possible

  3. Equivalent idioms to Scala where possible


Option, Either, and Array are complete. Please try it out, and let me know what you think!