| 1 |
module Kernel |
| 2 |
# call-seq: |
| 3 |
# obj.yield_self {|_obj|...} -> an_object |
| 4 |
# obj.then {|_obj|...} -> an_object |
| 5 |
# |
| 6 |
# Yields <i>obj</i> and returns the result. |
| 7 |
# |
| 8 |
# 'my string'.yield_self {|s|s.upcase} #=> "MY STRING" |
| 9 |
# |
| 10 |
def yield_self(&block) |
| 11 |
return to_enum :yield_self unless block |
| 12 |
block.call(self) |
| 13 |
end |
| 14 |
alias then yield_self |
| 15 |
|
| 16 |
## |
| 17 |
# call-seq: |
| 18 |
# obj.tap{|x|...} -> obj |
| 19 |
# |
| 20 |
# Yields <code>x</code> to the block, and then returns <code>x</code>. |
| 21 |
# The primary purpose of this method is to "tap into" a method chain, |
| 22 |
# in order to perform operations on intermediate results within the chain. |
| 23 |
# |
| 24 |
# (1..10) .tap {|x| puts "original: #{x.inspect}"} |
| 25 |
# .to_a .tap {|x| puts "array: #{x.inspect}"} |
| 26 |
# .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"} |
| 27 |
# .map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"} |
| 28 |
# |
| 29 |
def tap |
| 30 |
yield self |
| 31 |
self |
| 32 |
end |
| 33 |
end |