Class: Mon::Monad::Try

Inherits:
Mon::Monad show all
Includes:
ChainableMonad
Defined in:
lib/monads/try.rb

Direct Known Subclasses

Failure, Success

Class Method Summary collapse

Methods included from ChainableMonad

#_, #coerce, #method_missing, #respond_to?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mon::Monad::ChainableMonad

Class Method Details

.[](obj) ⇒ Object



12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/monads/try.rb', line 38

def self::valid?(o)
  o.is_a? self.class
end