Ruby 1.9 – Tap my Object

Posted by Arkham on October 28th, 2011

Let’ say you need to debug this piece of code:

very_cool_method(egg, sausage)

What would you do? I guess something along the lines of:

spam = very_cool_method(egg, sausage)
p spam

Well, Ruby 1.9 let you do something cooler instead:

very_cool_method(egg, sausage).tap{ |s| p s }

The cool thing about tap is that you can use it to eavesdrop method chains:

very_cool_method(egg, sausage).tap{ |s| p s }.now_make_sandwich_from(bacon)

Hooray! :)

This entry was posted on Friday, October 28th, 2011 at 09:53 and is filed under computers. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Tags: , , , ,

2 Responses to “Ruby 1.9 – Tap my Object”

  1. faber Says:

    p “Spam, lovely Spam, wonderful Spam.”

  2. dark Says:

    Tap: target player reveals its hand.