Tap in Rails 3 - Did I understand API Docs correctly?

I am upgrading a rails 2 application to rails 3.2 and am facing what is described as an idiom.

person.tap |p| do

When I googled for this and it seems to be out of date or moved . Do I understand correctly?

I ask because I can find some examples of this on SO.

+6
source share
2 answers

The tap method has been in Ruby since version 1.8.7 :

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

Allowed x to block, and then returns x . The main goal of this method is to β€œuse” the chain of methods in order to perform operations with intermediate results within the chain.

Note that 1.8.6 does not have Object#tap . Presumably tap was in older versions of Rails (like a monkey patch on Object ), but was added to Ruby in 1.8.7. Since version 1.8.6 is quite old, the version of Rails was deprecated and in later versions of Rails was completely removed.

1.9.3 still has Object#tap , so tap itself is not outdated, only the corrected version of Rails Monkey has been removed.

+10
source

The Monkey Object#tap patch from ActiveSupport deprecated because it was part of Ruby since 1.9.0 and 1.8.7.

+4
source

Source: https://habr.com/ru/post/912663/


All Articles