Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tap method in Rails 3 - Have I understood the API Docs correctly?

I'm upgrading a rails 2 application to rails 3.2 and have come across what is described as an idiom.

person.tap |p| do

When I Googled for this and it appears to have been deprecated or moved. Is my understanding correct?

I ask because I can find a few examples of it on SO.

like image 512
user1149642 Avatar asked Apr 07 '12 17:04

user1149642


2 Answers

The tap method has been in Ruby since 1.8.7:

tap{|x|...} => obj

Yields x to the block, and then returns x. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.

Note that 1.8.6 did not have Object#tap. Presumably, tap was in older versions of Rails (as a monkey patch on Object) but was added to Ruby itself in 1.8.7. Since 1.8.6 is rather ancient now, the Rails version was deprecated and, in more recent Rails releases, removed entirely.

Object#tap is still around so tap itself has not been deprecated, just the Rails monkey patched version has been removed.

like image 77
mu is too short Avatar answered Oct 14 '22 04:10

mu is too short


The Object#tap monkey patch from ActiveSupport is deprecated because it has been part of Ruby since 1.9.0 and 1.8.7.

like image 40
Jörg W Mittag Avatar answered Oct 14 '22 05:10

Jörg W Mittag