Maybelline

Maybe you should stop checking for nil and be confident

Build Status

An implementation of the maybe monad.

Setup

Gemfile

gem 'maybelline'

Manual

require 'maybelline'

Usage

Say you have some objects like the ones below:

require 'ostruct'

data = {
  "neat" => {
    "super" => "cool",
    "lame"  => nil
  }
}

object = OpenStruct.new(data)
object.neat = OpenStruct.new(object.neat)

If any method in the chain of calls returns nil, the block will return nil.

Maybe(data){|d| d["this_key_doesnt_exist"]["this_key_wont_get_called"]} #=> nil
data.maybe{|d| d["this_key_doesnt_exist"]["this_key_wont_get_called"]}  #=> nil
object.maybe{|o| o.neat.lame.this_method_never_gets_called}             #=> nil

If it is successful, it will return the value.

data.maybe{|d| d["neat"]["super"]}  #=> "cool"
object.maybe{|o| o.neat.super}      #=> "cool"

Note on Reporting Issues

Note on Patches/Pull Requests