public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Object#tap for Ruby < 1.8.7
jeremy (author)
Wed Jan 07 13:19:48 -0800 2009
commit  0f9e65b71f9af30dac17689e81f4353e9fcac5b6
tree    60b61c07e519ae2078575ec07ef7a030e4857467
parent  17da45b789e0a2581eae6e6b2b1ae8d2b98e0f5d
...
1
2
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
0
@@ -1,5 +1,8 @@
0
 *2.3.0 [Edge]*
0
 
0
+* Object#tap shim for Ruby < 1.8.7. Similar to Object#returning, tap yields self then returns self.  [Jeremy Kemper]
0
+    array.select { ... }.tap(&:inspect).map { ... }
0
+
0
 * TimeWithZone#- gives correct result with wrapped DateTime, and with DateTime argument [Geoff Buesing]
0
 
0
 * Updated i18n gem to version 0.1.1 #1635 [Yaroslav Markin]
...
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
44
45
...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
0
@@ -40,6 +40,21 @@ class Object
0
     value
0
   end
0
 
0
+  # Yields <code>x</code> to the block, and then returns <code>x</code>.
0
+  # The primary purpose of this method is to "tap into" a method chain,
0
+  # in order to perform operations on intermediate results within the chain.
0
+  #
0
+  #   (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
0
+  #     tap    { |x| puts "array: #{x.inspect}" }.
0
+  #     select { |x| x%2 == 0 }.
0
+  #     tap    { |x| puts "evens: #{x.inspect}" }.
0
+  #     map    { |x| x*x }.
0
+  #     tap    { |x| puts "squares: #{x.inspect}" }
0
+  def tap
0
+    yield self
0
+    self
0
+  end unless Object.respond_to?(:tap)
0
+
0
   # An elegant way to factor duplication out of options passed to a series of
0
   # method calls. Each method called in the block, with the block variable as
0
   # the receiver, will have its options merged with the default +options+ hash

Comments