The Tap Method
- 1 minOver the pass few days, I stumbled upon a method referred to as tap. It was very mind boggling, because I didn’t have any idea on what it was, or what its actual purpose was.
After going through numerous documentation, it made it pretty obvious that this method basically linked methods together; without actually changing the the value. Tap is unable to return anything, then what it is actually given. To be honest, the proper word is just CHAINING.
Now I know you all are probably saying, what is the point? That’s just useless, we’re only returning what the value is just given, right? But let us just think about it, this gives us the power to basically just “tap” into a method chain and create a tangential function.
So lets look at these examples:
(1..10).map {|x| x*x }.tap {|x| puts "array: #{x.length} items long"}.join
array: 10 items long
=> "149162536496481100"
user = User.new
user.username = "foobar"
user.save!
As a reader it takes me 3 lines to recognize that someone is just creating an instances for the class User. We also had to make a temporary variable.
So what if I just “tap that” ?
user = User.new.tap do |u|
u.username = "foobar"
u.save!
end
Look how much cleaner our code looks, and that temporary variable is GONE!
Tap gives us the ability, to access a part of the program that was really difficult to access. I really wonder what else can tap actually do ?