Class: Dry::Monads::Maybe::Some
- Inherits:
-
Dry::Monads::Maybe
- Object
- Dry::Monads::Maybe
- Dry::Monads::Maybe::Some
- Includes:
- RightBiased::Right
- Defined in:
- lib/dry/monads/maybe.rb,
lib/dry/monads/result.rb
Overview
Represents a value that is present, i.e. not nil.
Class Method Summary collapse
-
.[](*value) ⇒ Object
Shortcut for Some().
Instance Method Summary collapse
-
#filter(with = Undefined, &block) ⇒ Maybe::None, Maybe::Some
Accepts a block and runs it against the wrapped value.
-
#fmap ⇒ Maybe::Some, Maybe::None
Does the same thing as #bind except it also wraps the value in an instance of Maybe::Some monad.
-
#initialize(value = Undefined) ⇒ Some
constructor
A new instance of Some.
-
#maybe ⇒ Maybe::Some, Maybe::None
Does the same thing as #bind except it also wraps the value in an instance of the Maybe monad.
-
#to_result(_fail = Unit) ⇒ Success<Any>
Converts to Sucess(value!).
- #to_s ⇒ String (also: #inspect)
Methods included from RightBiased::Right
#===, #and, #apply, #bind, #deconstruct, #deconstruct_keys, #discard, #flatten, included, #or, #or_fmap, #tee, #value!, #value_or, #|
Methods inherited from Dry::Monads::Maybe
#as_json, coerce, json_create, #monad, #none?, pure, #some?, #to_json, #to_maybe, #to_monad, to_proc
Methods included from Transformer
Constructor Details
Class Method Details
Instance Method Details
#filter(with = Undefined, &block) ⇒ Maybe::None, Maybe::Some
Accepts a block and runs it against the wrapped value. If the block returns a trurhy value the result is self, otherwise None. If no block is given, the value serves and its result.
167 168 169 170 171 172 173 174 175 |
# File 'lib/dry/monads/maybe.rb', line 167 def filter(with = Undefined, &block) block = Undefined.default(with, block || IDENTITY) if block.(@value) self else Monads.None() end end |
#fmap ⇒ Maybe::Some, Maybe::None
Does the same thing as #bind except it also wraps the value in an instance of Maybe::Some monad. This allows for easier chaining of calls.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/dry/monads/maybe.rb', line 121 def fmap(...) next_value = bind(...) if next_value.nil? if self.class.warn_on_implicit_nil_coercion Core::Deprecations.warn( "Block passed to Some#fmap returned `nil` and was chained to None. "\ "This is literally an unlawful behavior and it will not be supported in "\ "dry-monads 2. \nPlease, replace `.fmap` with `.maybe` in places where you "\ "expect `nil` as block result.\n"\ "You can opt out of these warnings with\n"\ "Dry::Monads::Maybe.warn_on_implicit_nil_coercion false", uplevel: 0, tag: :"dry-monads" ) end Monads.None() else Some.new(next_value) end end |
#maybe ⇒ Maybe::Some, Maybe::None
Does the same thing as #bind except it also wraps the value in an instance of the Maybe monad. This allows for easier chaining of calls.
154 155 156 |
# File 'lib/dry/monads/maybe.rb', line 154 def maybe(...) Maybe.coerce(bind(...)) end |
#to_result(_fail = Unit) ⇒ Success<Any>
Converts to Sucess(value!)
408 409 410 |
# File 'lib/dry/monads/result.rb', line 408 def to_result(_fail = Unit) Result::Success.new(@value) end |
#to_s ⇒ String Also known as: inspect
178 179 180 181 182 183 184 |
# File 'lib/dry/monads/maybe.rb', line 178 def to_s if Unit.equal?(@value) "Some()" else "Some(#{@value.inspect})" end end |