diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-01-07 13:19:48 -0800 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-01-09 00:14:27 -0800 |
commit | c2d23affad0ed4542e3906c334a7b27b07fc695c (patch) | |
tree | 6045d4164363854a6dd917fcbf4af42a299bcfa8 /activesupport/lib | |
parent | b1530545d259c144770bd8fd7881cb16160c0afc (diff) | |
download | rails-c2d23affad0ed4542e3906c334a7b27b07fc695c.tar.gz rails-c2d23affad0ed4542e3906c334a7b27b07fc695c.tar.bz2 rails-c2d23affad0ed4542e3906c334a7b27b07fc695c.zip |
Object#tap for Ruby < 1.8.7
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/misc.rb | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index 46f9c7d676..4570570bbc 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -40,6 +40,21 @@ class Object value end + # Yields <code>x</code> to the block, and then returns <code>x</code>. + # The primary purpose of this method is to "tap into" a method chain, + # in order to perform operations on intermediate results within the chain. + # + # (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a. + # tap { |x| puts "array: #{x.inspect}" }. + # select { |x| x%2 == 0 }. + # tap { |x| puts "evens: #{x.inspect}" }. + # map { |x| x*x }. + # tap { |x| puts "squares: #{x.inspect}" } + def tap + yield self + self + end unless Object.respond_to?(:tap) + # An elegant way to factor duplication out of options passed to a series of # method calls. Each method called in the block, with the block variable as # the receiver, will have its options merged with the default +options+ hash |