Class: Monadify::Some
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #flatmap ⇒ Object
-
#initialize(val) ⇒ Some
constructor
A new instance of Some.
- #map ⇒ Object
Methods inherited from Option
Constructor Details
#initialize(val) ⇒ Some
Returns a new instance of Some.
6 7 8 |
# File 'lib/monadify/option/some.rb', line 6 def initialize(val) @value = val end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
4 5 6 |
# File 'lib/monadify/option/some.rb', line 4 def value @value end |
Instance Method Details
#empty? ⇒ Boolean
10 11 12 |
# File 'lib/monadify/option/some.rb', line 10 def empty? false end |
#flatmap ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/monadify/option/some.rb', line 29 def flatmap begin return_option = yield value case return_option when Monadify::None return_option when Monadify::Some Monadify::Some.new(return_option.value) else raise NotAnOptionError end rescue NotAnOptionError raise ArgumentError, 'The block should return an Option' rescue ArgumentError, NameError, TypeError raise rescue Monadify::None.new end end |
#map ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/monadify/option/some.rb', line 14 def map begin return_val = yield value if return_val.nil? Monadify::None.new else Monadify::Some.new(return_val) end rescue ArgumentError, NameError, TypeError raise rescue Monadify::None.new end end |