Replying to /2019/01/07/disclaimer-i-dont-like-tap.html
I’d love to see better examples of using #tap
, but I’ve mostly seen it used to refactor this:
metadata = {}
metadata[:id] = user.id if user
metadata
That’s explicit. It creates a hash, adds a value if a condition is true, then returns the hash.
With #tap
, the code is more difficult to read, and requires the next person to know what #tap
does (“yields x to the block, and then returns x.”). 🤷♀️
{}.tap do |metadata|
metadata[:id] = user.id if user
end