<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Budding Rubyist</title>
	<atom:link href="http://buddingrubyist.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://buddingrubyist.com</link>
	<description>tidbits and best practices in ruby and rails development</description>
	<lastBuildDate>Mon, 31 Oct 2011 14:21:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='buddingrubyist.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The Budding Rubyist</title>
		<link>http://buddingrubyist.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://buddingrubyist.com/osd.xml" title="The Budding Rubyist" />
	<atom:link rel='hub' href='http://buddingrubyist.com/?pushpress=hub'/>
		<item>
		<title>Tweak your Spotlight Settings to Double OSX Battery Life</title>
		<link>http://buddingrubyist.com/2010/06/08/tweak-your-spotlight-settings-to-double-osx-battery-life/</link>
		<comments>http://buddingrubyist.com/2010/06/08/tweak-your-spotlight-settings-to-double-osx-battery-life/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 20:54:22 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://buddingrubyist.com/?p=33</guid>
		<description><![CDATA[My Spotlight indexer process was consistently using 75-150% CPU. Since I *never* use Spotlight for document content searches, and *only* use it for app launching (really, Alfred (http://alfredapp.com) on top of Spotlight), I added ALL hard drive directories except for my Applications directory to Spotlight&#8217;s Privacy tab (System Preferences &#62; Spotlight &#62; Privacy). The Spotlight [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=33&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>My Spotlight indexer process was consistently using 75-150% CPU.  Since I *never* use Spotlight for document content searches, and *only* use it for app launching (really, Alfred (<a href="http://alfredapp.com">http://alfredapp.com</a>) on top of Spotlight), I added ALL hard drive directories except for my Applications directory to Spotlight&#8217;s Privacy tab (System Preferences &gt; Spotlight &gt; Privacy).</p>
<p>The Spotlight indexer process is now silent, and my battery life has doubled.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=33&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2010/06/08/tweak-your-spotlight-settings-to-double-osx-battery-life/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
		<item>
		<title>How to speed up gem installs 10x</title>
		<link>http://buddingrubyist.com/2009/02/14/how-to-speed-up-gem-installs-10x/</link>
		<comments>http://buddingrubyist.com/2009/02/14/how-to-speed-up-gem-installs-10x/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 02:44:09 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://buddingrubyist.com/?p=29</guid>
		<description><![CDATA[Answer: Turn off ri and rdoc installation. Sure, you can do this on the commandline, like so: sudo gem install fastercsv --no-ri --no-rdoc Better, though, is to set this up as the default in your ~/.gemrc file. --- :verbose: true :sources: - http://gems.rubyforge.org/ - http://gems.github.com/ :update_sources: true :backtrace: false :bulk_threshold: 1000 :benchmark: false gem: --no-ri [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=29&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Answer: Turn off ri and rdoc installation.  Sure, you can do this on the commandline, like so:</p>
<pre>sudo gem install fastercsv --no-ri --no-rdoc</pre>
<p>Better, though, is to set this up as the default in your ~/.gemrc file.</p>
<pre>---
:verbose: true
:sources:
- http://gems.rubyforge.org/
- http://gems.github.com/
:update_sources: true
:backtrace: false
:bulk_threshold: 1000
:benchmark: false
<strong>gem: --no-ri --no-rdoc</strong>
</pre>
<p>Just add that last line and your gem installations will complete in no time.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/29/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=29&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2009/02/14/how-to-speed-up-gem-installs-10x/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
		<item>
		<title>alias_method_chaining your AR associations (for fun and profit?)</title>
		<link>http://buddingrubyist.com/2008/10/14/alias_method_chaining-your-ar-associations-for-fun-and-profit/</link>
		<comments>http://buddingrubyist.com/2008/10/14/alias_method_chaining-your-ar-associations-for-fun-and-profit/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 07:36:42 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://buddingrubyist.wordpress.com/?p=27</guid>
		<description><![CDATA[I&#8217;ve used alias_method_chain on top of ActiveRecord associations in a couple of situations lately.  It has emerged as kind of a neat pattern, imho. For Caching Let&#8217;s say that you&#8217;ve got some Users who have Roles.  These roles don&#8217;t change frequently at all, so they are prime candidates for caching. When calling User#roles, the aliased [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=27&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve used <a href="http://weblog.rubyonrails.org/2006/4/26/new-in-rails-module-alias_method_chain">alias_method_chain</a> on top of ActiveRecord associations in a couple of situations lately.  It has emerged as kind of a neat pattern, imho.</p>
<p><strong>For Caching</strong><br />
Let&#8217;s say that you&#8217;ve got some Users who have Roles.  These roles don&#8217;t change frequently at all, so they are prime candidates for caching.</p>
<pre class="brush: ruby;">

class User &lt; ActiveRecord::Base

  has_many :roles

  def roles_with_cache
    Rails.cache.fetch(&quot;user_roles/#{self.id}&quot;, :expires_in =&gt; 15.minutes) do
      roles_without_cache
    end
  end
  alias_method_chain :roles, :cache

end
</pre>
<p>When calling User#roles, the aliased method will try to fetch them from the cache first.  If they don&#8217;t exist there, they&#8217;ll be pulled from the database.</p>
<p><strong>For Sharding</strong><br />
If you&#8217;re using the excellent <a href="http://github.com/fiveruns/data_fabric/tree/master">DataFabric</a> gem to shard your database, you know that your access (finders, etc.) to the sharded tables is done within a block.  We can use the same pattern from above to auto-shard access to these associated objects.</p>
<pre class="brush: ruby;">

class User &lt; ActiveRecord::Base

  has_many :tweets

  def tweets_with_shard
    self.in_shard { tweets_without_shard }
  end
  alias_method_chain :tweets, :shard

  def in_shard(&amp;block)
    DataFabric.activate_shard(:store =&gt; shard, &amp;block)
  end

end
</pre>
<p>Fun.  What other interesting uses for alias_method_chain on top of ActiveRecord associations have you seen?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=27&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2008/10/14/alias_method_chaining-your-ar-associations-for-fun-and-profit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
		<item>
		<title>A Change in Protocol: Exploring XMPP in Ruby</title>
		<link>http://buddingrubyist.com/2008/09/29/a-change-in-protocol-exploring-xmpp-in-ruby/</link>
		<comments>http://buddingrubyist.com/2008/09/29/a-change-in-protocol-exploring-xmpp-in-ruby/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 03:32:20 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[talks]]></category>
		<category><![CDATA[ruby xmpp talk]]></category>

		<guid isPermaLink="false">http://buddingrubyist.wordpress.com/?p=23</guid>
		<description><![CDATA[I gave a talk on XMPP at Austin on Rails this past Tuesday (Sept 23, 2008).  Many thanks to all who attended. I&#8217;ve finally gotten around to cleaning up my slide deck and most of the code I showed. You can find the slides HERE on SlideShare. You can grab the code HERE on GitHub.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=23&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I gave a talk on XMPP at <a href="http://www.austinonrails.org/articles/2008/09/16/meeting-tuesday-september-23-2008-7-9-pm">Austin on Rails</a> this past Tuesday (Sept 23, 2008).  Many thanks to all who attended. I&#8217;ve finally gotten around to cleaning up my slide deck and most of the code I showed.</p>
<p>You can find the slides <a href="http://www.slideshare.net/bdainton/a-change-in-protocol-exploring-xmpp-in-ruby-presentation">HERE</a> on SlideShare.</p>
<p>You can grab the code <a href="http://github.com/bdainton/xmpp_talk">HERE</a> on GitHub.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=23&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2008/09/29/a-change-in-protocol-exploring-xmpp-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
		<item>
		<title>CampTweet 0.8.4 Released</title>
		<link>http://buddingrubyist.com/2008/08/16/camptweet-084-released/</link>
		<comments>http://buddingrubyist.com/2008/08/16/camptweet-084-released/#comments</comments>
		<pubDate>Sat, 16 Aug 2008 16:29:50 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://buddingrubyist.wordpress.com/?p=21</guid>
		<description><![CDATA[A while ago, I wrote a post on the FiveRuns blog about the first public release of the CampTweet gem.  In short, this gem enables you to pipe Twitter statuses, Twitter search results, and content from RSS feeds into a Campfire room. It&#8217;s awesome.  Well, it *was* awesome. And then came the throttling.  Twitter throttling, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=21&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A while ago, I wrote a post on the FiveRuns blog about <a href="http://blog.fiveruns.com/2008/7/7/introducing-camptweet">the first public release of the CampTweet gem</a>.  In short, this gem enables you to pipe Twitter statuses, Twitter search results, and content from RSS feeds into a <a href="http://www.campfirenow.com">Campfire</a> room.</p>
<p>It&#8217;s awesome.  Well, it *was* awesome.</p>
<p>And then came the throttling.  Twitter throttling, that is, of unauthenticated GET requests against users&#8217; public timelines.  This change meant that, if CampTweet polled Twitter more than 100 times in an hour for your teammates&#8217; statuses, all subsequent requests from that IP received an error: Twitter REST Error (Bad Request).</p>
<p>Well&#8230;although it&#8217;s not a completely ideal solution, I&#8217;ve updated the camptweet gem to adjust its polling interval to keep it under the 100 reqs/hour limit.  After using this for a couple of weeks now at FiveRuns, the number of &#8216;Bad Request&#8217; responses from Twitter has dropped dramatically and the turnaround on getting tweets into Campfire has greatly improved.</p>
<p>If you&#8217;re using CampTweet, I highly recommend that you get this update.  If you&#8217;re not using CampTweet, well&#8230;you should be.</p>
<p><code>sudo gem install camptweet</code></p>
<p>Enjoy!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/buddingrubyist.wordpress.com/21/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/buddingrubyist.wordpress.com/21/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=21&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2008/08/16/camptweet-084-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
		<item>
		<title>Oh wait, I meant to assign you to something</title>
		<link>http://buddingrubyist.com/2008/05/21/irb-underscore/</link>
		<comments>http://buddingrubyist.com/2008/05/21/irb-underscore/#comments</comments>
		<pubDate>Wed, 21 May 2008 04:59:56 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[irb]]></category>

		<guid isPermaLink="false">http://buddingrubyist.wordpress.com/?p=19</guid>
		<description><![CDATA[While doing some exploratory surgery via script/console the other day, I got clued into a neat little feature of irb: the underscore.  In short, it&#8217;s a special variable tied to the previous result. &#62;&#62; Team.find(:all, :conditions =&#62; 'games_played &#62; 500') =&#62; # [...a bunch of teams here...] Oops.  I need to do a couple of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=19&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While doing some exploratory surgery via script/console the other day, I got clued into a neat little feature of irb: the underscore.  In short, it&#8217;s a special variable tied to the previous result.</p>
<pre><span class="source source_ruby"><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">&gt;&gt;</span> <span class="support support_class support_class_ruby">Team</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>find<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>all</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>conditions</span> <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>games_played &gt; 500<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span>
<span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> [...a bunch of teams here...]</span></span></pre>
<p>Oops.  I need to do a couple of operations on this set of teams.  Instead of retyping the above or going up/back in my irb history and changing the start of the line, I&#8217;ll just use the underscore. </p>
<pre><span class="source source_ruby"><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">&gt;&gt;</span> teams <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> _</span></pre>
<p>Now, I can do what I originally wanted. </p>
<pre><span class="source source_ruby"><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">&gt;&gt;</span> winners <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> teams<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>select <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{</span><span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block"> </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">team</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span> team<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>winning_percentage <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">&gt;</span> <span class="constant constant_numeric constant_numeric_ruby">0.80</span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">}</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>map<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">&amp;</span><span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>name</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span>
<span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">&gt;&gt;</span> losers <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> teams<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>select <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{</span><span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block"> </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">team</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span> team<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>winning_percentage <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">&lt;</span> <span class="constant constant_numeric constant_numeric_ruby">0.20</span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">}</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>map<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">&amp;</span><span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>name</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span></span></pre>
<p>Handy.</p>
<p> </p>
<p> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/buddingrubyist.wordpress.com/19/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/buddingrubyist.wordpress.com/19/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=19&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2008/05/21/irb-underscore/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
		<item>
		<title>A Peek into Daily Development at FiveRuns</title>
		<link>http://buddingrubyist.com/2008/03/06/a-peek-into-daily-development-at-fiveruns/</link>
		<comments>http://buddingrubyist.com/2008/03/06/a-peek-into-daily-development-at-fiveruns/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 20:46:14 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[fiveruns]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://buddingrubyist.wordpress.com/?p=18</guid>
		<description><![CDATA[I wrote a post today for the FiveRuns corporate blog entitled &#8216;Beyond Continuous Integration: Continuous Information&#8216;.  It&#8217;s a quick look at some of the tools and practices we use daily on the development team.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=18&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I wrote a post today for the FiveRuns corporate blog entitled &#8216;<a href="http://blog.fiveruns.com/2008/3/6/beyond-continuous-integration-continuous-information">Beyond Continuous Integration: Continuous Information</a>&#8216;.  It&#8217;s a quick look at some of the tools and practices we use daily on the development team.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/buddingrubyist.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/buddingrubyist.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=18&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2008/03/06/a-peek-into-daily-development-at-fiveruns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
		<item>
		<title>&#8216;Testing for Class Equality in a Case Statement&#8217; or &#8216;How to Get Pwned by ===&#8217;</title>
		<link>http://buddingrubyist.com/2008/02/18/testing-for-class-equality-in-a-case-statement-or-how-to-get-pwned-by/</link>
		<comments>http://buddingrubyist.com/2008/02/18/testing-for-class-equality-in-a-case-statement-or-how-to-get-pwned-by/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 07:41:43 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby case]]></category>

		<guid isPermaLink="false">http://buddingrubyist.wordpress.com/?p=14</guid>
		<description><![CDATA[I recently wrote a case statement (case/when) in which the branching condition was based on a passed-in class name. Simplified, it looked something like this: class CommandExecutor def execute(command) case command when StopClientCommand.name print "Stopping the Client..." when StartClientCommand.name print "Starting the Client..." when DisableClientCommand.name print "Disabling the Client..." else puts "Can't handle command: #{command}" [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=14&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently wrote a case statement (case/when) in which the branching condition was based on a passed-in class name.  Simplified, it looked something like this:</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:5px;"><span class="source source_ruby"><span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">CommandExecutor</span></span>

  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">execute</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">command</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    <span class="keyword keyword_control keyword_control_ruby">case</span> command
    <span class="keyword keyword_control keyword_control_ruby">when</span> <span class="support support_class support_class_ruby">StopClientCommand</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>name
      print <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Stopping the Client...<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
    <span class="keyword keyword_control keyword_control_ruby">when</span> <span class="support support_class support_class_ruby">StartClientCommand</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>name
      print <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Starting the Client...<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
    <span class="keyword keyword_control keyword_control_ruby">when</span> <span class="support support_class support_class_ruby">DisableClientCommand</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>name
      print <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Disabling the Client...<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
    <span class="keyword keyword_control keyword_control_ruby">else</span>
      puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Can't handle command: <span class="source source_ruby source_ruby_embedded source_ruby_embedded_source"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">#{</span>command<span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">}</span></span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
      <span class="keyword keyword_control keyword_control_pseudo-method keyword_control_pseudo-method_ruby">return</span>
    <span class="keyword keyword_control keyword_control_ruby">end</span>

    puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>DONE.<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">StopClientCommand</span></span><span class="punctuation punctuation_separator punctuation_separator_statement punctuation_separator_statement_ruby">;</span> <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">StartClientCommand</span></span><span class="punctuation punctuation_separator punctuation_separator_statement punctuation_separator_statement_ruby">;</span> <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">DisableClientCommand</span></span><span class="punctuation punctuation_separator punctuation_separator_statement punctuation_separator_statement_ruby">;</span> <span class="keyword keyword_control keyword_control_ruby">end</span>

executor <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="support support_class support_class_ruby">CommandExecutor</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span>
executor<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>execute <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>StopClientCommand<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>
executor<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>execute <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>StartClientCommand<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>
executor<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>execute <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>DisableClientCommand<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span></span></pre>
<p>This code totally worked&#8230;</p>
<pre style="background:#cc9966 none repeat scroll 0 0;color:#000000;padding:5px;"><span class="source source_ruby"><span class="variable variable_other variable_other_constant variable_other_constant_ruby">Stopping</span> the <span class="variable variable_other variable_other_constant variable_other_constant_ruby">Client</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">...</span><span class="variable variable_other variable_other_constant variable_other_constant_ruby">DONE</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>
<span class="variable variable_other variable_other_constant variable_other_constant_ruby">Starting</span> the <span class="variable variable_other variable_other_constant variable_other_constant_ruby">Client</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">...</span><span class="variable variable_other variable_other_constant variable_other_constant_ruby">DONE</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>
<span class="variable variable_other variable_other_constant variable_other_constant_ruby">Disabling</span> the <span class="variable variable_other variable_other_constant variable_other_constant_ruby">Client</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">...</span><span class="variable variable_other variable_other_constant variable_other_constant_ruby">DONE</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span></span></pre>
<p>&#8230;but it gnawed at me that I was passing a string representation of the class name.  It just felt dirty.</p>
<p>I pinged <a href="http://www.codefluency.com">a colleague</a> for input, and his reaction was immediate: &#8220;Oh, don&#8217;t pass a string; just pass and check for the actual class.  It&#8217;s the more idiomatic way of doing class-checking.&#8221;</p>
<p>Ah, good to know.  So&#8230;I made a couple of tweaks:</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:5px;"><span class="source source_ruby"><span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">CommandExecutor</span></span>

  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">execute</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">command</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    <span class="keyword keyword_control keyword_control_ruby">case</span> command
    <span class="keyword keyword_control keyword_control_ruby">when</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">StopClientCommand</span>    <span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> &lt;-- check against a class, not a class name
</span>      print <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Stopping the Client...<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
    <span class="keyword keyword_control keyword_control_ruby">when</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">StartClientCommand</span>
      print <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Starting the Client...<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
    <span class="keyword keyword_control keyword_control_ruby">when</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">DisableClientCommand</span>
      print <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Disabling the Client...<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
    <span class="keyword keyword_control keyword_control_ruby">else</span>
      puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Can’t handle command: <span class="source source_ruby source_ruby_embedded source_ruby_embedded_source"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">#{</span>command<span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">}</span></span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
      <span class="keyword keyword_control keyword_control_pseudo-method keyword_control_pseudo-method_ruby">return</span>
    <span class="keyword keyword_control keyword_control_ruby">end</span>

    puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>DONE.<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">StopClientCommand</span></span><span class="punctuation punctuation_separator punctuation_separator_statement punctuation_separator_statement_ruby">;</span> <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">StartClientCommand</span></span><span class="punctuation punctuation_separator punctuation_separator_statement punctuation_separator_statement_ruby">;</span> <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">DisableClientCommand</span></span><span class="punctuation punctuation_separator punctuation_separator_statement punctuation_separator_statement_ruby">;</span> <span class="keyword keyword_control keyword_control_ruby">end</span>

executor <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="support support_class support_class_ruby">CommandExecutor</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span>
executor<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>execute <span class="variable variable_other variable_other_constant variable_other_constant_ruby">StopClientCommand</span>    <span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> &lt;-- pass a class, not a string
</span>executor<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>execute <span class="variable variable_other variable_other_constant variable_other_constant_ruby">StartClientCommand</span>
executor<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>execute <span class="variable variable_other variable_other_constant variable_other_constant_ruby">DisableClientCommand</span></span></pre>
<p>And ran it&#8230;</p>
<pre style="background:#cc9966 none repeat scroll 0 0;color:#000000;padding:5px;"><span class="source source_ruby"><span class="variable variable_other variable_other_constant variable_other_constant_ruby">Can</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>t handle command: StopClientCommand
Can<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>t handle command<span class="punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby">:</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">StartClientCommand</span>
<span class="variable variable_other variable_other_constant variable_other_constant_ruby">Can</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>t handle command: DisableClientCommand</span></span></pre>
<p>Um.  What the heck?  Maybe I fat-fingered something in there?  I threw in a little ghetto-debugging for a sanity check and got the same results:</p>
<pre style="background:#cc9966 none repeat scroll 0 0;color:#000000;padding:5px;"><span class="source source_ruby"><span class="variable variable_other variable_other_constant variable_other_constant_ruby">Passed</span><span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">-</span><span class="keyword keyword_control keyword_control_ruby">in</span> command   <span class="punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby">:</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">StopClientCommand</span>
<span class="variable variable_other variable_other_constant variable_other_constant_ruby">Can</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>t handle command: StopClientCommand
Passed-in command   : StartClientCommand
Can<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>t handle command<span class="punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby">:</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">StartClientCommand</span>
<span class="variable variable_other variable_other_constant variable_other_constant_ruby">Passed</span><span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">-</span><span class="keyword keyword_control keyword_control_ruby">in</span> command   <span class="punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby">:</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">DisableClientCommand</span>
<span class="variable variable_other variable_other_constant variable_other_constant_ruby">Can</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>t handle command: DisableClientCommand</span></span></pre>
<p>The reason?</p>
<p>Case statements are evaluated using the <a href="http://www.ruby-doc.org/core/classes/Object.html#M000343">Object#===</a> (Case Equality) method which, <a href="http://www.ruby-doc.org/core/classes/Object.html#M000343">according to the documentation</a>, is &#8220;typically overridden by descendents [sic] to provide meaningful semantics in case statements.&#8221;</p>
<p>Any chance that the Class class overrides this method?  You betcha.  Its case-equality behavior is to evaluate whether the case condition <b>represents an instance of that class</b>, NOT if it is a matching class definition.</p>
<p>With that in mind, I made one last change:</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:5px;"><span class="source source_ruby"><span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">CommandExecutor</span></span>

  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">execute</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">command</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    <span class="keyword keyword_control keyword_control_ruby">case</span> command<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span>  <span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> &lt;-- check against an instance of the class
</span>    <span class="keyword keyword_control keyword_control_ruby">when</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">StopClientCommand</span>
      print <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Stopping the Client...<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
    <span class="keyword keyword_control keyword_control_ruby">when</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">StartClientCommand</span>
      print <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Starting the Client...<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
    <span class="keyword keyword_control keyword_control_ruby">when</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">DisableClientCommand</span>
      print <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Disabling the Client...<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
    <span class="keyword keyword_control keyword_control_ruby">else</span>
      puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Can't handle command: <span class="source source_ruby source_ruby_embedded source_ruby_embedded_source"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">#{</span>command<span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">}</span></span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
      <span class="keyword keyword_control keyword_control_pseudo-method keyword_control_pseudo-method_ruby">return</span>
    <span class="keyword keyword_control keyword_control_ruby">end</span>

    puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>DONE.<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="keyword keyword_control keyword_control_ruby">end</span></span></pre>
<p>And voila:</p>
<pre style="background:#cc9966 none repeat scroll 0 0;color:#000000;padding:5px;"><span class="source source_ruby"><span class="variable variable_other variable_other_constant variable_other_constant_ruby">Stopping</span> the <span class="variable variable_other variable_other_constant variable_other_constant_ruby">Client</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">...</span><span class="variable variable_other variable_other_constant variable_other_constant_ruby">DONE</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>
<span class="variable variable_other variable_other_constant variable_other_constant_ruby">Starting</span> the <span class="variable variable_other variable_other_constant variable_other_constant_ruby">Client</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">...</span><span class="variable variable_other variable_other_constant variable_other_constant_ruby">DONE</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>
<span class="variable variable_other variable_other_constant variable_other_constant_ruby">Disabling</span> the <span class="variable variable_other variable_other_constant variable_other_constant_ruby">Client</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">...</span><span class="variable variable_other variable_other_constant variable_other_constant_ruby">DONE</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span></span></pre>
<p>We finally have the correct output, and we&#8217;re using idiomatic Ruby to get there.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/buddingrubyist.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/buddingrubyist.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=14&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2008/02/18/testing-for-class-equality-in-a-case-statement-or-how-to-get-pwned-by/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
		<item>
		<title>Why I like to inject</title>
		<link>http://buddingrubyist.com/2008/02/05/why-i-like-to-inject/</link>
		<comments>http://buddingrubyist.com/2008/02/05/why-i-like-to-inject/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 05:01:23 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby inject]]></category>

		<guid isPermaLink="false">http://buddingrubyist.wordpress.com/?p=12</guid>
		<description><![CDATA[Enumerable#inject, that is. I know that there are a lot of Budding Rubyists out there. Are you a former (or current) Java developer? That was me, but I saw the light and have moved on to greener pastures. A big step in the transition from Java to Ruby was the adoption of inject as a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=12&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Enumerable#inject, that is.</p>
<p>I know that there are a lot of Budding Rubyists out there.  Are you a former (or current) Java developer?  That was me, but I <a href="http://www.infoworld.com/article/07/12/28/52FE-underreported-java_1.html">saw the light</a> and have moved on to <a href="http://www.fiveruns.com">greener pastures</a>.</p>
<p>A big step in the transition from Java to Ruby was the adoption of <code>inject</code> as a first-class weapon in my iteration arsenal.  So, what&#8217;s the big deal, and how does it work?  Let&#8217;s break it down.</p>
<p>You&#8217;re going to use <code>inject</code> to iterate through some values and use each of them to <b>manipulate some other object</b>. Let&#8217;s say that we have an array of two-element arrays (a la [['a', 1], ['b', 2], ['c', 3]]) and want to construct a hash with the first element as the key and the second element as the value ({ &#8216;a&#8217; =&gt; 1, &#8216;b&#8217; =&gt; 2, &#8216;c&#8217; =&gt; 3}).   Someone just transitioning to Ruby might do this:</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:10px;"><span class="source source_ruby">hash <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{}</span>
array<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>each <span class="keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby">do </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">current</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span>
  hash<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span>current<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span><span class="constant constant_numeric constant_numeric_ruby">0</span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]]</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> current<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span><span class="constant constant_numeric constant_numeric_ruby">1</span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]</span>
<span class="keyword keyword_control keyword_control_ruby">end</span></span></pre>
<p>You can do this with <code>inject</code> like so:</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:10px;"><span class="source source_ruby">hash <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> array<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>inject<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{}</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span> <span class="keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby">do </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">results</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">,</span> <span class="variable variable_other variable_other_block variable_other_block_ruby">current</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span>
  results<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span>current<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span><span class="constant constant_numeric constant_numeric_ruby">0</span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]]</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> current<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span><span class="constant constant_numeric constant_numeric_ruby">1</span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]</span>
  results
<span class="keyword keyword_control keyword_control_ruby">end</span></span></pre>
<p>Ok, to be fair&#8230;this code is not simpler.  Not as pretty, imho.  More Ruby-esque?  Sure.  Patience, Grasshopper.</p>
<p>You can think of <code>inject</code> as a replacement for <code>each</code> that takes an argument representing the starting state of the object you want to manipulate.  We want to construct a hash, so this starting state is <code>{}</code>. The block, then, receives another argument (I call it &#8216;results&#8217; above): the value of your object passed in from the previous iteration.</p>
<p>One key thing to remember when using <code>inject</code>: you <b>must</b> return the &#8216;results&#8217; from the iteration at the end of the block.  Not doing so is a very common mistake that will totally break you.</p>
<p>Let&#8217;s pick a different example: we want to sum the values in an array.  Old way:</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:10px;"><span class="source source_ruby">total <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="constant constant_numeric constant_numeric_ruby">0</span>
array<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>each <span class="keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby">do </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">current</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span>
  total <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby">+=</span> current
<span class="keyword keyword_control keyword_control_ruby">end</span>
total</span></pre>
<p>With <code>inject</code>:</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:10px;"><span class="source source_ruby">total <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> array<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>inject<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="constant constant_numeric constant_numeric_ruby">0</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span> <span class="keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby">do </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">total</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">,</span> <span class="variable variable_other variable_other_block variable_other_block_ruby">current</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span>
  total <span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">+</span> current
<span class="keyword keyword_control keyword_control_ruby">end</span></span></pre>
<p>Again, you mentally translate the above to:<br />
- start with 0<br />
- iterate through each value, adding the current value to the total, and<br />
- pass the total on to the next iteration</p>
<p>Easy peasy.  (What, you just want to use ActiveSupport&#8217;s <code>Enumerable#sum</code>?  Bleh.  Fair enough.)  Ok, one more.  This one actually shows it used in a realistic scenario.</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:10px;"><span class="source source_ruby"><span class="support support_class support_class_ruby">Array</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>class_eval <span class="keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby">do
</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Construct a hash of objects, keyed by some object attribute.
</span>  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">hash_by</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">attribute</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    inject<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{}</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span> <span class="keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby">do </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">results</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">,</span> <span class="variable variable_other variable_other_block variable_other_block_ruby">obj</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span>
      results<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span>obj<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>send<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span>attribute<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> obj
      results
    <span class="keyword keyword_control keyword_control_ruby">end</span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="keyword keyword_control keyword_control_ruby">end</span></span></pre>
<p>Here, I have reopened the Array class to add a new method, hash_by().  I use it to construct a cache of objects so that I can quickly look up a specific object using a unique object attribute.  Here it is in action:</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:10px;"><span class="source source_ruby"><span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">User</span></span>
  <span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">attr_accessor</span> <span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>name</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>age</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>email</span>

  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">initialize</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">name<span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> age<span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> email</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    <span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby">@</span>name</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> name
    <span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby">@</span>age</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> age
    <span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby">@</span>email</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> email
  <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="keyword keyword_control keyword_control_ruby">end</span><span class="keyword keyword_control keyword_control_ruby"></span>

users <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span><span class="support support_class support_class_ruby">User</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Brian<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="constant constant_numeric constant_numeric_ruby">32</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>brian@foo.bar.com<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
          <span class="support support_class support_class_ruby">User</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Jim<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="constant constant_numeric constant_numeric_ruby">46</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>jim@foo.bar.com<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
          <span class="support support_class support_class_ruby">User</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Scott<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="constant constant_numeric constant_numeric_ruby">33</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>scott@foo.bar.com<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
          <span class="support support_class support_class_ruby">User</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Kenton<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="constant constant_numeric constant_numeric_ruby">32</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>kenton@foo.bar.com<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
          <span class="support support_class support_class_ruby">User</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Chris<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="constant constant_numeric constant_numeric_ruby">34</span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>chris@foo.bar.com<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]</span>

user_cache <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> users<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>hash_by<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>name</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span>

puts user_cache<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Brian<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>inspect
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> =&gt; #&lt;User:0x28514 @email="brian@foo.bar.com", @age=32, @name="Brian"&gt;
</span>
puts user_cache<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Scott<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>inspect
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> =&gt; #&lt;User:0x2844c @email="scott@foo.bar.com", @age=33, @name="Scott"&gt;</span></span></pre>
<p>With the constructed User cache, we can now get to the object of our choice in constant time.  Yay, <code>inject</code>!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/buddingrubyist.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/buddingrubyist.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=12&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2008/02/05/why-i-like-to-inject/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
		<item>
		<title>Don&#8217;t blindly chain destructive(!) Array methods</title>
		<link>http://buddingrubyist.com/2007/12/06/dont-blindly-chain-destructive-array-methods/</link>
		<comments>http://buddingrubyist.com/2007/12/06/dont-blindly-chain-destructive-array-methods/#comments</comments>
		<pubDate>Thu, 06 Dec 2007 21:17:05 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://buddingrubyist.wordpress.com/2007/12/06/dont-blindly-chain-destructive-array-methods/</guid>
		<description><![CDATA[Certain methods on the Array class (map!/collect!, compact!, uniq!, etc.) perform in-place modification of an array&#8217;s contents. I think they&#8217;re slick. Be careful, though: contexts.map! {&#124;context&#124; context.controller}.compact!.uniq! The result? undefined method `uniq!' for nil:NilClass Yep, compact! was returning nil. From the documentation: &#8220;Returns nil if no changes were made.&#8221; Well&#8230;bleh. I&#8217;d rather it had returned [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=11&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Certain methods on the Array class (<a href="http://ruby-doc.org/core/classes/Array.html#M002213">map!/collect!</a>, <a href="http://ruby-doc.org/core/classes/Array.html#M002240">compact!</a>, <a href="http://ruby-doc.org/core/classes/Array.html#M002238">uniq!</a>, etc.) perform in-place modification of an array&#8217;s contents.  I think they&#8217;re slick.  Be careful, though:</p>
<p><code>  contexts.map! {|context| context.controller}.compact!.uniq!</code></p>
<p>The result?</p>
<p><code>  undefined method `uniq!' for nil:NilClass</code></p>
<p>Yep, compact! was returning nil.  From the documentation: &#8220;Returns <code>nil</code> if no changes were made.&#8221;</p>
<p>Well&#8230;bleh.  I&#8217;d rather it had returned the (unchanged) array.</p>
<p>I understand the intent of the API&#8217;s design (explicitly let the caller know that there were no changes made to the input array) , but it just wasn&#8217;t what I expected.</p>
<p>So&#8230;be careful.<code></code></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/buddingrubyist.wordpress.com/11/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/buddingrubyist.wordpress.com/11/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/buddingrubyist.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/buddingrubyist.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/buddingrubyist.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/buddingrubyist.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/buddingrubyist.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/buddingrubyist.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/buddingrubyist.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/buddingrubyist.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/buddingrubyist.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/buddingrubyist.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/buddingrubyist.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/buddingrubyist.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/buddingrubyist.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/buddingrubyist.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=buddingrubyist.com&amp;blog=1977519&amp;post=11&amp;subd=buddingrubyist&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://buddingrubyist.com/2007/12/06/dont-blindly-chain-destructive-array-methods/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9ea9b4ca5025de41f3d491adde9e4b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brian</media:title>
		</media:content>
	</item>
	</channel>
</rss>
