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.
153 154 155 156 157 158 159 160 161 |
# File 'lib/dry/monads/maybe.rb', line 153 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.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dry/monads/maybe.rb', line 109 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.
142 |
# File 'lib/dry/monads/maybe.rb', line 142 def maybe(...) = Maybe.coerce(bind(...)) |
#to_result(_fail = Unit) ⇒ Success<Any>
Converts to Sucess(value!)
374 |
# File 'lib/dry/monads/result.rb', line 374 def to_result(_fail = Unit, &) = Result::Success.new(@value) |
#to_s ⇒ String Also known as: inspect
164 165 166 167 168 169 170 |
# File 'lib/dry/monads/maybe.rb', line 164 def to_s if Unit.equal?(@value) "Some()" else "Some(#{@value.inspect})" end end |