<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: Tag tap</title>
    <link>http://blog.moertel.com/articles/tag/tap?tag=tap</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>Ruby 1.9 gets handy new method Object#tap</title>
      <description>&lt;p&gt;Via
&lt;a href="http://eigenclass.org/hiki.rb?Changes-in-Ruby-1.9-update-6"&gt;eigenclass.org&lt;/a&gt;
I learned that Ruby 1.9 will sport a new &lt;code&gt;Object&lt;/code&gt; method
called
&lt;a href="http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l25"&gt;&lt;code&gt;tap&lt;/code&gt;&lt;/a&gt;,
which is something I&amp;#8217;ve been &lt;a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/189541"&gt;hoping
for&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;What&amp;#8217;s &lt;code&gt;tap&lt;/code&gt;?  It&amp;#8217;s a helper for call chaining.  It
passes its object into the given block and, after the block finishes,
returns the object:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;an_object&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;tap&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;o&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
  &lt;span class="comment"&gt;# do stuff with an_object, which is in o&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt; &lt;span class="comment"&gt;# ===&amp;gt; an_object&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The benefit is that &lt;code&gt;tap&lt;/code&gt; always returns the object it&amp;#8217;s called on, even if the block returns some other result.  Thus you can insert a &lt;code&gt;tap&lt;/code&gt; block into the middle of an existing method pipleline without breaking the flow.  MenTaLguY has some &lt;a href="http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions"&gt;nifty examples&lt;/a&gt; of other things you can do with &lt;code&gt;tap&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Fans of Ruby on Rails may recognize &lt;code&gt;tap&lt;/code&gt; as similar to RoR&amp;#8217;s own
&lt;a href="http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object-returning"&gt;&lt;code&gt;returning&lt;/code&gt;&lt;/a&gt; helper.&lt;/p&gt;


	&lt;p&gt;Looks like Ruby 1.9 is going to be extra cool for a number of reasons.&lt;/p&gt;</description>
      <pubDate>Wed, 07 Feb 2007 12:08:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:9a2e5bfe-f2b1-462f-88c5-cd231503292a</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2007/02/07/ruby-1-9-gets-handy-new-method-object-tap</link>
      <category>ruby</category>
      <category>rails</category>
      <category>ruby</category>
      <category>rails</category>
      <category>tap</category>
      <category>helpers</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/362</trackback:ping>
    </item>
  </channel>
</rss>
