Module: Corefines::Object::Then
- Defined in:
- lib/corefines/object.rb
Instance Method Summary collapse
-
#then {|self| ... } ⇒ Object
Returns
selfifselfevaluate tofalse, otherwise returns evaluation of the block.
Instance Method Details
#then {|self| ... } ⇒ Object
Returns self if self evaluate to false, otherwise returns
evaluation of the block.
This simplifies something like:
if m = "Flynn <[email protected]>".match(/<([^>]+)>/)
m[1]
end
to:
"Flynn <[email protected]>".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") }
233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/corefines/object.rb', line 233 module Then refine ::Object do def then if block_given? && self yield self else self end end end end |