Class: Maybe
- Inherits:
-
Object
- Object
- Maybe
- Defined in:
- lib/maybe.rb,
lib/maybe/version.rb
Overview
Wrap a value into a Maybe monad (or Optional type). Since monads are chainable and every value is wrapped in one, things don’t blow up with NoMethodError on nils.
Useful for retrieving something in deeply nested structures where nils could be produced along the way.
Constant Summary collapse
- VERSION =
Version number, happy now?
'0.0.1'
Instance Method Summary collapse
-
#chain {|value| ... } ⇒ Maybe
Chain another action and return a new value wrapped in a new
Maybeor the sameMaybeif the old value is anil. -
#initialize(value) ⇒ Maybe
constructor
Wrap a value into a new
Maybe. -
#method_missing(*args, &block) ⇒ Maybe
Chain another action and return a new value wrapped in a new
Maybeor the sameMaybeif the old value is anil. -
#respond_to_missing?(method_name, include_private = false) ⇒ Bool
Also support
respond_to?. -
#unwrap ⇒ Object
Return the wrapped value.
Constructor Details
#initialize(value) ⇒ Maybe
Wrap a value into a new Maybe.
44 45 46 |
# File 'lib/maybe.rb', line 44 def initialize(value) @value = value end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Maybe
Chain another action and return a new value wrapped in a new Maybe or the same Maybe if the old value is a nil. Calls chain under the hood.
69 70 71 72 73 |
# File 'lib/maybe.rb', line 69 def method_missing(*args, &block) chain do |value| value.public_send(*args, &block) end end |
Instance Method Details
#chain {|value| ... } ⇒ Maybe
Chain another action and return a new value wrapped in a new Maybe or the same Maybe if the old value is a nil.
104 105 106 107 108 109 110 111 112 |
# File 'lib/maybe.rb', line 104 def chain # Don't call the block if we are wrapping a nil (hint: that would cause # a +NoMethodError+). return self if @value.nil? # Call the passed block to get the new value and wrap it again. new_value = yield @value self.class.new(new_value) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Bool
Also support respond_to?.
82 83 84 |
# File 'lib/maybe.rb', line 82 def respond_to_missing?(method_name, include_private = false) super || @value.respond_to?(method_name, include_private) end |
#unwrap ⇒ Object
Return the wrapped value.
56 57 58 |
# File 'lib/maybe.rb', line 56 def unwrap @value end |