Class: Monad::Maybe::Just
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
41
42
43
|
# File 'lib/monad/maybe/just.rb', line 41
def equal?(other)
other.__id__ == self.__id__
end
|
45
46
47
|
# File 'lib/monad/maybe/just.rb', line 45
def inspect
"just(#{value.inspect})"
end
|
#just? ⇒ Boolean
25
26
27
|
# File 'lib/monad/maybe/just.rb', line 25
def just?
true
end
|
#nil? ⇒ Boolean
29
30
31
|
# File 'lib/monad/maybe/just.rb', line 29
def nil?
false
end
|
#nothing? ⇒ Boolean
21
22
23
|
# File 'lib/monad/maybe/just.rb', line 21
def nothing?
false
end
|
53
54
55
|
# File 'lib/monad/maybe/just.rb', line 53
def to_a
[self]
end
|
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
|