class Present

A class that represents a wrapped value. This class holds something that is not nil.

Public Instance Methods

==(other) click to toggle source

Is this wrapped value equal to the given wrapped value?

> 1.wrapped == 1.wrapped > nil.wrapped == 2.wrapped

# File lib/wrapped/present.rb, line 113
def ==(other)
  other.is_a?(Present) && unwrap == other.unwrap_or(nil)
end
blank(&ignored) click to toggle source

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}

# File lib/wrapped/present.rb, line 34
def blank(&ignored)
  self
end
blank?() click to toggle source

False; this does not wrap a nil.

# File lib/wrapped/present.rb, line 81
def blank?
  false
end
collect()
Alias for: fmap
each() { |unwrap| ... } click to toggle source

Produces itself.

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}” }

# File lib/wrapped/present.rb, line 45
def each
  yield unwrap if block_given?
  self
end
find_all()
Alias for: select
flat_map() { |unwrap| ... } click to toggle source

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 }

# File lib/wrapped/present.rb, line 93
def flat_map
  yield unwrap
end
fmap() { |unwrap)| ... } click to toggle source

Run a block within the wrapper. This produces a wrapped value.

> w.fmap {|n| n+1 }

This makes it a functor.

# File lib/wrapped/present.rb, line 102
def fmap
  Present.new(yield unwrap)
end
Also aliased as: collect, map
grep(*) click to toggle source

Produces itself if the unwrapped value matches the given expression. Produces Blank otherwise.

Calls superclass method
# File lib/wrapped/present.rb, line 66
def grep(*)
  super.first.wrapped
end
map()
Alias for: fmap
present(&block) click to toggle source

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?” }

# File lib/wrapped/present.rb, line 25
def present(&block)
  block.call(unwrap)
  self
end
present?() click to toggle source

True; this is an instance of a wrapped value.

# File lib/wrapped/present.rb, line 76
def present?
  true
end
reject() click to toggle source

Produces itself if the block evaluates to false. Produces Blank if the block evaluates to true.

Calls superclass method
# File lib/wrapped/present.rb, line 60
def reject
  super.first.wrapped
end
select() click to toggle source

Produces itself if the block evaluates to true. Produces Blank if the block evaluates to false.

Calls superclass method
# File lib/wrapped/present.rb, line 52
def select
  super.first.wrapped
end
Also aliased as: find_all
unwrap() click to toggle source

The raw value. I doubt you need this method.

# File lib/wrapped/present.rb, line 71
def unwrap
  @value
end
unwrap_or(_default = nil) click to toggle source

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}” }

# File lib/wrapped/present.rb, line 17
def unwrap_or(_default = nil)
  unwrap
end