Class: Mon::Monad::None

Inherits:
Maybe show all
Defined in:
lib/monads/maybe.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Maybe

#method_missing, #orFail, #valid?

Methods included from ChainableMonad

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

Constructor Details

#initializeNone

Returns a new instance of None.



184
185
186
# File 'lib/monads/maybe.rb', line 184

def initialize
  super()
end

Dynamic Method Handling

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

Class Method Details

.[]Object

Return a new None object. Takes no args. None[] # ==> None



176
177
178
# File 'lib/monads/maybe.rb', line 176

def self.[]
  None.new
end

.noneObject



200
201
202
# File 'lib/monads/maybe.rb', line 200

def self::none
  self.new
end

Instance Method Details

#==(o) ⇒ Object



192
193
194
# File 'lib/monads/maybe.rb', line 192

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

#bind(&fun) ⇒ Object

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



145
146
147
# File 'lib/monads/maybe.rb', line 145

def bind &fun
  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/monads/maybe.rb', line 188

def eql?(other)
  other.is_a? None
end

#equal?(o) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/monads/maybe.rb', line 196

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

#or(obj = nil, &f) ⇒ Object

If there’s a wrapped value, return it. Otherwise, either return the supplied object, or execute the supplied block. Eg: Maybe[1].or(5) # ==> 1 Maybe[nil].or(5) # ==> 5 Maybe[nil].or { throw Exception.new("...") } # ==> Exception!



160
161
162
163
164
165
166
167
168
# File 'lib/monads/maybe.rb', line 160

def or obj = nil, &f
  if obj and f
    f.call obj
  elsif f
    f.call
  else
    obj
  end
end

#to_sObject



180
181
182
# File 'lib/monads/maybe.rb', line 180

def to_s
  "None"
end