Class: Some
Overview
Represents a non-empty value
Instance Method Summary
collapse
Methods inherited from Maybe
#inspect, #to_ary
Constructor Details
#initialize(value, inst_method = nil, parent_stack = []) ⇒ Some
Returns a new instance of Some.
84
85
86
87
88
|
# File 'lib/possibly.rb', line 84
def initialize(value, inst_method = nil, parent_stack = [])
@value = value
@inst_method = inst_method || ["Some.new", []]
@parent_stack = parent_stack
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args, &block) ⇒ Object
125
126
127
128
|
# File 'lib/possibly.rb', line 125
def method_missing(method_sym, *args, &block)
@invocation ||= [method_sym, args, block]
map { |value| value.send(method_sym, *args, &block) }
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
rubocop:enable PredicateName
116
117
118
|
# File 'lib/possibly.rb', line 116
def ==(other)
super && get == other.get
end
|
#===(other) ⇒ Object
121
122
123
|
# File 'lib/possibly.rb', line 121
def ===(other)
other && other.class == self.class && @value === other.get
end
|
#get ⇒ Object
90
91
92
|
# File 'lib/possibly.rb', line 90
def get
@value
end
|
#is_none? ⇒ Boolean
111
112
113
|
# File 'lib/possibly.rb', line 111
def is_none?
false
end
|
#is_some? ⇒ Boolean
rubocop:disable PredicateName
107
108
109
|
# File 'lib/possibly.rb', line 107
def is_some?
true
end
|
#or_else ⇒ Object
94
95
96
|
# File 'lib/possibly.rb', line 94
def or_else(*)
@value
end
|
#or_nil ⇒ Object
102
103
104
|
# File 'lib/possibly.rb', line 102
def or_nil
@value
end
|
#or_raise ⇒ Object
98
99
100
|
# File 'lib/possibly.rb', line 98
def or_raise(*)
@value
end
|
#to_s ⇒ Object
130
131
132
|
# File 'lib/possibly.rb', line 130
def to_s
"Some(#{@value})"
end
|