module Corefines::Object::Then

@!method then

Returns +self+ if +self+ evaluate to +false+, otherwise returns
evaluation of the block.

This simplifies something like:

  if m = "Flynn <flynn@encom.com>".match(/<([^>]+)>/)
    m[1]
  end

to:

  "Flynn <flynn@encom.com>".match(/<([^>]+)>/).then { |m| m[1] }

Since +then+ passes +self+ to the block, it can be also used for
chaining, so something like:

  html = parse_html(input)
  html = find_nodes(html, "//section")
  html = remove_nodes(html, "//p")

can be rewritten to:

  parse_html(input)
    .then { |h| find_nodes(h, "//section") }
    .then { |h| remove_nodes(h, "//p") }

@yield [self] gives +self+ to the block.
@return [Object] evaluation of the block, or +self+ if no block given
  or +self+ evaluates to false.

Public Instance Methods

then() { |self| ... } click to toggle source
# File lib/corefines/object.rb, line 235
def then
  if block_given? && self
    yield self
  else
    self
  end
end