class Mon::Monad::Some

The Some class represents a value that is NOT null/false

Public Class Methods

[](obj) click to toggle source

Wrap an object. You probably want Maybe, but there's a slight difference: Maybe # ==> None Some # ==> Some

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

Protected Class Methods

new(obj) click to toggle source
Calls superclass method
# File lib/monads/maybe.rb, line 62
def initialize(obj)
  @obj = obj
  super()
end

Public Instance Methods

==(o) click to toggle source
# File lib/monads/maybe.rb, line 127
def ==(o)
  eql?(o)
end
bind(&fun) click to toggle source

If we're wrapping a value, apply fun() to it. Otherwise, None stays None.

# File lib/monads/maybe.rb, line 104
def bind &fun
  o = fun.call(@obj)
  if o.is_a? Maybe
    o
  elsif o
    Some[o]
  else
    None::none
  end
end
eql?(other) click to toggle source
# File lib/monads/maybe.rb, line 119
def eql?(other)
  if other.is_a? Some
    @obj == other.unwrap
  else
    @obj == other
  end
end
equal?(o) click to toggle source
# File lib/monads/maybe.rb, line 131
def equal?(o)
  eql?(o)
end
or(obj = nil, &f) click to toggle source

If there's a wrapped value, return it. Otherwise, either return the supplied object, or execute the supplied block. Eg: <tt>Maybe.or(5) # ==> 1 Maybe.or(5) # ==> 5 Maybe.or { throw Exception.new(“…”) } # ==> Exception!

# File lib/monads/maybe.rb, line 93
def or obj = nil, &f
  @obj
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 98
def orFail(msg = nil)
  msg = "No such value!" if msg.nil?
  self::or { throw RuntimeError.new(msg) }
end
to_s() click to toggle source
# File lib/monads/maybe.rb, line 115
def to_s
  "Some[#{ @obj.to_s }]"
end

Protected Instance Methods

_canBind?(name) click to toggle source
# File lib/monads/maybe.rb, line 83
def _canBind? name
  @obj.respond_to? name
end
unwrap() click to toggle source

Unwrap the wrapped value, nil or not

# File lib/monads/maybe.rb, line 79
def unwrap
  @obj
end