Class: Monad::Maybe::Just

Inherits:
Base show all
Defined in:
lib/monad/maybe/just.rb

Overview

Wraps a non-nil object allows us to treat these objects as a Maybe while distinguishing them from a Nothing

Instance Attribute Summary

Attributes inherited from Base

#value

Instance Method Summary collapse

Methods inherited from Base

#<<, #maybe, #maybe?, #to_list

Constructor Details

#initialize(value) ⇒ Just

Returns a new instance of Just.



9
10
11
# File 'lib/monad/maybe/just.rb', line 9

def initialize(value)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



13
14
15
# File 'lib/monad/maybe/just.rb', line 13

def method_missing(method, *args)
  Just.new(value.send(method, *args))
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
# File 'lib/monad/maybe/just.rb', line 33

def ==(other)
  self === other || self.value == other
end

#===(other) ⇒ Object



37
38
39
# File 'lib/monad/maybe/just.rb', line 37

def ===(other)
  other.just? && self.value == other.value
end

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/monad/maybe/just.rb', line 41

def equal?(other)
  other.__id__ == self.__id__
end

#inspectObject



45
46
47
# File 'lib/monad/maybe/just.rb', line 45

def inspect
  "just(#{value.inspect})"
end

#just?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/monad/maybe/just.rb', line 25

def just?
  true
end

#nil?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/monad/maybe/just.rb', line 29

def nil?
  false
end

#nothing?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/monad/maybe/just.rb', line 21

def nothing?
  false
end

#to_aObject



53
54
55
# File 'lib/monad/maybe/just.rb', line 53

def to_a
  [self]
end

#to_sObject



49
50
51
# File 'lib/monad/maybe/just.rb', line 49

def to_s
  Just.new(value.to_s)
end

#unwrap(val) ⇒ Object



17
18
19
# File 'lib/monad/maybe/just.rb', line 17

def unwrap(val)
  value
end