Class: Present
Overview
A class that represents a wrapped value. This class holds something that is not nil.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Is this wrapped value equal to the given wrapped value?.
-
#blank(&ignored) ⇒ Object
Do nothing then produce the wrapped value, making it chainable.
-
#blank? ⇒ Boolean
False; this does not wrap a nil.
-
#each {|unwrap| ... } ⇒ Object
Produce the singleton list with the unwrapped value as its only member.
-
#flat_map {|unwrap| ... } ⇒ Object
Run a block against the unwrapped value, producing the result of the block.
-
#fmap ⇒ Object
Run a block within the wrapper.
-
#initialize(value) ⇒ Present
constructor
Use Object#wrapped and NilClass#wrapped instead.
-
#present(&block) ⇒ Object
Invoke the block on the value, unwrapped.
-
#present? ⇒ Boolean
True; this is an instance of a wrapped value.
-
#unwrap ⇒ Object
The raw value.
-
#unwrap_or(_) ⇒ Object
Produce the value, unwrapped.
Constructor Details
#initialize(value) ⇒ Present
Use Object#wrapped and NilClass#wrapped instead.
7 8 9 |
# File 'lib/wrapped/present.rb', line 7 def initialize(value) # :nodoc: @value = value end |
Instance Method Details
#==(other) ⇒ Object
Is this wrapped value equal to the given wrapped value?
> 1.wrapped == 1.wrapped > nil.wrapped == 2.wrapped
94 95 96 |
# File 'lib/wrapped/present.rb', line 94 def ==(other) unwrap == other.unwrap_or(nil) end |
#blank(&ignored) ⇒ Object
Do nothing then produce the wrapped value, making it chainable. See ‘present’ for its companion.
> w.blank { puts “Symbol not found” }.present {|s| puts users}
38 39 40 |
# File 'lib/wrapped/present.rb', line 38 def blank(&ignored) self end |
#blank? ⇒ Boolean
False; this does not wrap a nil.
65 66 67 |
# File 'lib/wrapped/present.rb', line 65 def blank? false end |
#each {|unwrap| ... } ⇒ Object
Produce the singleton list with the unwrapped value as its only member.
If a block is passed, it is run against the unwrapped value.
This class mixes in Enumerable, which is controlled by this method.
> w.each {|n| puts “Found #{n}” }
49 50 51 52 |
# File 'lib/wrapped/present.rb', line 49 def each yield unwrap if block_given? [unwrap] end |
#flat_map {|unwrap| ... } ⇒ Object
Run a block against the unwrapped value, producing the result of the block.
> w.flat_map {|n| n+1 }
Also, you can use this like you would use >>= in Haskell. This and wrapped make it a monad.
> w.flat_map {|n| (n+1).wrapped }
77 78 79 |
# File 'lib/wrapped/present.rb', line 77 def flat_map yield unwrap end |
#fmap ⇒ Object
Run a block within the wrapper. This produces a wrapped value.
> w.fmap {|n| n+1 }
This makes it a functor.
86 87 88 |
# File 'lib/wrapped/present.rb', line 86 def fmap (yield unwrap).wrapped end |
#present(&block) ⇒ Object
Invoke the block on the value, unwrapped. This method produces the wrapped value again, making it chainable. See ‘blank’ for its companion.
> w.present {|s| puts “Hello, #{s}” }.blank { puts “Do I know you?” }
29 30 31 32 |
# File 'lib/wrapped/present.rb', line 29 def present(&block) block.call(unwrap) self end |
#present? ⇒ Boolean
True; this is an instance of a wrapped value.
60 61 62 |
# File 'lib/wrapped/present.rb', line 60 def present? true end |
#unwrap ⇒ Object
The raw value. I doubt you need this method.
55 56 57 |
# File 'lib/wrapped/present.rb', line 55 def unwrap @value end |
#unwrap_or(_) ⇒ Object
Produce the value, unwrapped.
If a block is given apply the block to the value and produce that.
> w.unwrap_or(0) > w.unwrap_or(“hello”) {|s| “Hi, #{s}” }
17 18 19 20 21 22 23 |
# File 'lib/wrapped/present.rb', line 17 def unwrap_or(_) if block_given? yield unwrap else unwrap end end |