Class: Mon::Monad::Success

Inherits:
Try show all
Defined in:
lib/monads/try.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Try

to

Methods included from ChainableMonad

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

Constructor Details

#initialize(obj) ⇒ Success

Returns a new instance of Success.



49
50
51
52
53
54
55
56
# File 'lib/monads/try.rb', line 49

def initialize(obj)
  if obj.is_a? Success
    @obj = obj.get
  else
    @obj = obj
  end
  super()
end

Dynamic Method Handling

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

Class Method Details

.[](obj) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/monads/try.rb', line 58

def self.[](obj)
  if (obj.is_a? Failure) or (obj.is_a? Success)
    obj
  else
    Success.new(obj)
  end
end

.valid?(o) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/monads/try.rb', line 110

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

Instance Method Details

#==(o) ⇒ Object



98
99
100
# File 'lib/monads/try.rb', line 98

def ==(o)
  eql?(o)
end

#bind(&fun) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/monads/try.rb', line 66

def bind &fun
  begin
    Success[fun.call(@obj)]
  rescue StandardError => e
    Failure[e]
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/monads/try.rb', line 90

def eql?(other)
  if other.is_a? Success
    @obj == other.unwrap
  else
    @obj == other
  end
end

#equal?(o) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/monads/try.rb', line 102

def equal?(o)
  eql?(o)
end

#or(obj, &f) ⇒ Object



78
79
80
# File 'lib/monads/try.rb', line 78

def or obj, &f
  @obj
end

#orFailObject



82
83
84
# File 'lib/monads/try.rb', line 82

def orFail
  @obj
end

#to_sObject



106
107
108
# File 'lib/monads/try.rb', line 106

def to_s
  "Success[#{@obj}]"
end