class Mon::Monad::Try

Public Class Methods

[](obj) click to toggle source
# File lib/monads/try.rb, line 12
def self.[](obj)
  if obj.is_a? Failure
    Failure[obj.get]
  elsif obj
    Success[obj]
  else
    Failure[false]
  end
end
to(*obj, &f) click to toggle source
# File lib/monads/try.rb, line 22
def self::to *obj, &f
  begin
    if obj.length == 1 and obj[0].is_a? Proc
      raise RuntimeError("Provided too many procs!") if f
      Success[obj.call]
    elsif f and obj.length > 0
      Success[f.call(*obj)]
    else
      raise RuntimeError("Failed to provide block") if not f
      Success[f.call]
    end
  rescue StandardError => e
    Failure[e]
  end
end
valid?(o) click to toggle source
# File lib/monads/try.rb, line 38
def self::valid?(o)
  o.is_a? self.class
end