Class: Mon::Monad::Failure

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(err) ⇒ Failure

Returns a new instance of Failure.



123
124
125
# File 'lib/monads/try.rb', line 123

def initialize(err)
  @err = err
end

Dynamic Method Handling

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

Class Method Details

.[](err) ⇒ Object



127
128
129
# File 'lib/monads/try.rb', line 127

def self.[](err)
  self.new(err)
end

.valid?(o) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/monads/try.rb', line 179

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

Instance Method Details

#==(o) ⇒ Object



171
172
173
# File 'lib/monads/try.rb', line 171

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

#bind(&fun) ⇒ Object



131
132
133
# File 'lib/monads/try.rb', line 131

def bind &fun
  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/monads/try.rb', line 167

def eql?(other)
  (other.is_a? Failure) ? (@err == other.unwrap) : false
end

#equal?(o) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/monads/try.rb', line 175

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

#or(obj, &f) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/monads/try.rb', line 135

def or obj, &f
  if f and obj
    f.call(obj)
  elsif f
    f.call
  else
    obj
  end
end

#orFail(&f) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/monads/try.rb', line 145

def orFail &f
  if f
    f.call(@err)
  elsif @err.is_a? Exception
    raise @err
  else
    @err
  end
end

#to_sObject



163
164
165
# File 'lib/monads/try.rb', line 163

def to_s
  "Failure[#{@err}]"
end