Maybe you should stop checking for nil and be confident
MIT License
An implementation of the maybe monad.
Gemfile
gem 'maybelline'
Manual
require 'maybelline'
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"