class Mon::Monad::Maybe

Superclass for Some and None. Can be used as follows: m = Maybe[nil] # ==> None m = Maybe[5] # ==> Some[5] m = Maybe[nil] * 7 # ==> None m = Maybe[5] * 7 # ==> Some[35] m = Maybe[call_to_fun].someOperation(3) # ==> Some[...] or None, never an error

Public Class Methods

[](obj) click to toggle source

Use to instantiate a Maybe monad: m = Maybe[<either nil/false or not>]

# File lib/monads/maybe.rb, line 33
def self.[](obj)
  if obj.is_a? Maybe
    obj
  elsif obj
    Some[obj]
  else
    None[]
  end
end

Public Instance Methods

method_missing(name, *args, &fun) click to toggle source

Override to catch None

# File lib/monads/maybe.rb, line 27
def method_missing(name, *args, &fun)
  self.bind { |o| o.send(name, *args, &fun) }
end
orFail(msg = nil) click to toggle source

Get the value, or throw an exception (using the optional supplied msg) if it's empty

# File lib/monads/maybe.rb, line 44
def orFail(msg = nil)
  msg = "#{ self } is empty!" unless msg
  throw RuntimeError.new(msg)
end
valid?(o) click to toggle source

For Contracts, DEPRECATED

# File lib/monads/maybe.rb, line 50
def valid?(o)
  o.is_a? Maybe
end