<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.1" -->
<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/"
	>

<channel>
	<title>Ruby, a message to you</title>
	<link>http://ruby.tie-rack.org</link>
	<description>Ruby.send(:stop, 'messing around')</description>
	<pubDate>Mon, 05 May 2008 20:41:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>
	<language>en</language>
			<item>
		<title>DevFi podcast #1!</title>
		<link>http://ruby.tie-rack.org/66/devfi-podcast-1/</link>
		<comments>http://ruby.tie-rack.org/66/devfi-podcast-1/#comments</comments>
		<pubDate>Mon, 05 May 2008 20:41:03 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[DevFi]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/66/devfi-podcast-1/</guid>
		<description><![CDATA[Peter Jones (good friend, awesome developer) and I started a podcast, DevFi, and we released our first episode just moments ago. We do a little high level talk about buying vs. building vs. forking, and then jump into an interview with Dan Berger, the guy who forked the Ruby interpreter to create the Sapphire programming [...]]]></description>
			<content:encoded><![CDATA[<p>Peter Jones (good friend, awesome developer) and I started a podcast, <a href="http://devfi.com/">DevFi</a>, and we released our <a href="http://devfi.com/articles/2008/05/05/devfi-podcast-1-buy-build-fork">first episode</a> just moments ago. We do a little high level talk about buying vs. building vs. forking, and then jump into an interview with Dan Berger, the guy who forked the Ruby interpreter to create the <a href="http://sapphire-lang.org/">Sapphire programming language</a>.</p>
<p>The podcast isn&#8217;t Ruby-centric (like this blog), but it should still be worthwhile. Hope you enjoy it. (We&#8217;re also taking comments and suggestions if you don&#8217;t enjoy it enough.)</p>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/66/devfi-podcast-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Will Rubinius have fewer dark corners?</title>
		<link>http://ruby.tie-rack.org/65/will-rubinius-have-fewer-dark-corners/</link>
		<comments>http://ruby.tie-rack.org/65/will-rubinius-have-fewer-dark-corners/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 04:01:34 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[rubinius]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/65/will-rubinius-have-fewer-dark-corners/</guid>
		<description><![CDATA[Section 3.4.2 of The Ruby Programming Language talks about defining (or overriding) the hash and eql? methods of a class in order to control how objects are treated as keys in a hash&#8230; except for strings. Strings are handled specially because they&#8217;re &#8220;mutable but commonly used as hash keys.&#8221;
So when someone asked today on comp.lang.ruby [...]]]></description>
			<content:encoded><![CDATA[<p>Section 3.4.2 of <a href="http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/" title="by David Flanagan and Matz">The Ruby Programming Language</a> talks about defining (or overriding) the <code>hash</code> and <code>eql?</code> methods of a class in order to control how objects are treated as keys in a hash&#8230; except for strings. Strings are handled specially because they&#8217;re &#8220;mutable but commonly used as hash keys.&#8221;</p>
<p>So when someone asked today on comp.lang.ruby about <a href="http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/3733fe04f22130e1">case-insensitive hash access</a>, the answer, sadly, could not be as simple (and possibly dangerous) as &#8220;Just override <code>hash</code> and <code>eql?</code> in <code>String</code>.&#8221; Or could it?</p>
<p>Well, I whipped up something quickly:</p>
<pre><span class="keyword">class </span><span class="class">String</span>
  <span class="keyword">alias</span> <span class="ident">old_hash</span> <span class="ident">hash</span>
  <span class="keyword">def </span><span class="method">hash</span>
    <span class="constant">self</span><span class="punct">.</span><span class="ident">downcase</span><span class="punct">.</span><span class="ident">old_hash</span>
  <span class="keyword">end</span>

  <span class="keyword">def </span><span class="method">eql?</span><span class="punct">(</span><span class="ident">other</span><span class="punct">)</span>
    <span class="constant">self</span><span class="punct">.</span><span class="ident">hash</span> <span class="punct">==</span> <span class="ident">other</span><span class="punct">.</span><span class="ident">hash</span>
  <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="ident">require</span> <span class="punct">&#8216;</span><span class="string">test/unit</span><span class="punct">&#8216;</span>

<span class="keyword">class </span><span class="class">TestCaseInsensitiveHashAccess</span> <span class="punct">&lt;</span> <span class="constant">Test</span><span class="punct">::</span><span class="constant">Unit</span><span class="punct">::</span><span class="constant">TestCase</span>
  <span class="keyword">def </span><span class="method">test_case_insensitivity</span>
    <span class="ident">h</span> <span class="punct">=</span> <span class="punct">{&#8217;</span><span class="string">a</span><span class="punct">&#8216;</span> <span class="punct">=&gt;</span> <span class="number">1</span><span class="punct">,</span> <span class="punct">&#8216;</span><span class="string">B</span><span class="punct">&#8216;</span> <span class="punct">=&gt;</span> <span class="punct">&#8216;</span><span class="string">buzz</span><span class="punct">&#8216;}</span>
    <span class="ident">assert_equal</span><span class="punct">(</span><span class="number">1</span><span class="punct">,</span> <span class="ident">h</span><span class="punct">[&#8217;</span><span class="string">A</span><span class="punct">&#8216;])</span>
    <span class="ident">assert_equal</span><span class="punct">(&#8217;</span><span class="string">buzz</span><span class="punct">&#8216;,</span> <span class="ident">h</span><span class="punct">[&#8217;</span><span class="string">b</span><span class="punct">&#8216;])</span>
  <span class="keyword">end</span>
<span class="keyword">end</span></pre>
<p>I ran it a few times. Ruby failed. Ruby 1.9 failed. JRuby failed. But Rubinius passed.</p>
<p>And why shouldn&#8217;t it pass? For every other class, this sort of monkeying with <code>hash</code> and <code>eql?</code> will get you what you want: hash key access mayhem. Why not <code>String</code>?</p>
<p>Well, there might be something sinister hiding in the &#8220;mutable but commonly used as hash keys&#8221; description. Something subtle. Maybe it&#8217;s not subtle and I just haven&#8217;t been thinking very clearly.  I don&#8217;t know.</p>
<p>It looks to me, in the short amount of time I&#8217;ve been thinking about this, that Rubinius is more consistently Ruby-ish than Ruby is (at least in this case). Are there more ways in which that&#8217;s true?</p>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/65/will-rubinius-have-fewer-dark-corners/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Evil: knowing when a method got passed the default value</title>
		<link>http://ruby.tie-rack.org/64/evil-knowing-when-a-method-got-passed-the-default-value/</link>
		<comments>http://ruby.tie-rack.org/64/evil-knowing-when-a-method-got-passed-the-default-value/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 19:33:23 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[metaprogramming]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/64/evil-knowing-when-a-method-got-passed-the-default-value/</guid>
		<description><![CDATA[Let&#8217;s say you&#8217;re crazy. Now, as a crazy person, you might have a method like this:
def greet_world(salutation=&#8221;Hello&#8220;)
  &#8220;#{salutation} World!&#8220;
end
You&#8217;re not so crazy that you can&#8217;t write working Ruby code. Anyway, it&#8217;s totally useful:
greet_world          # =&#62; &#8220;Hello World!&#8221;
greet_world(&#8217;Hi&#8216;)    # =&#62; &#8220;Hi World!&#8221;
greet_world(&#8217;Yo&#8216;)  [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you&#8217;re crazy. Now, as a crazy person, you might have a method like this:</p>
<pre><span class="keyword">def </span><span class="method">greet_world</span><span class="punct">(</span><span class="ident">salutation</span><span class="punct">=&#8221;</span><span class="string">Hello</span><span class="punct">&#8220;)</span>
  <span class="punct">&#8220;</span><span class="string"><span class="expr">#{salutation}</span> World!</span><span class="punct">&#8220;</span>
<span class="keyword">end</span></pre>
<p>You&#8217;re not so crazy that you can&#8217;t write working Ruby code. Anyway, it&#8217;s totally useful:</p>
<pre><span class="ident">greet_world</span>          <span class="comment"># =&gt; &#8220;Hello World!&#8221;</span>
<span class="ident">greet_world</span><span class="punct">(&#8217;</span><span class="string">Hi</span><span class="punct">&#8216;)</span>    <span class="comment"># =&gt; &#8220;Hi World!&#8221;</span>
<span class="ident">greet_world</span><span class="punct">(&#8217;</span><span class="string">Yo</span><span class="punct">&#8216;)</span>    <span class="comment"># =&gt; &#8220;Yo World!&#8221;</span>
<span class="ident">greet_world</span><span class="punct">(&#8217;</span><span class="string">Hola</span><span class="punct">&#8216;)</span>  <span class="comment"># =&gt; &#8220;Hola World!&#8221;</span></pre>
<p>But then you see someone do this:</p>
<pre><span class="ident">greet_world</span><span class="punct">(&#8217;</span><span class="string">Hello</span><span class="punct">&#8216;)</span>  <span class="comment"># =&gt; &#8220;Hello World!&#8221;</span></pre>
<p>And it drives you even crazier. Being totally crazy, you feel a compulsion to make fun of people who pass the default value for an argument of a method. But how in the world can you tell?</p>
<p>Well, it turns out (and you probably know this already) that the argument list of a method is a proper venue for executing code. You know this because you&#8217;ve seen this a thousand times: <code>def something(time=Time.now)</code>. That <code>Time.now</code> happens when you call the method without that optional argument. It doesn&#8217;t happen when Ruby first parses the method and adds it to the method table for the class. That wouldn&#8217;t be too helpful.</p>
<p>So&#8230; you can execute arbitrary code in the method argument list. But what kind? What&#8217;s the scope? Well, anything you can do in a method body is fair game (and anything you can&#8217;t is not). You can&#8217;t define a class, for instance. And the scope? The method body itself.</p>
<p>You&#8217;re crazy, remember? But not so crazy that you don&#8217;t remember that every statement has a return value. Like assignment. Assignment returns a value. And you&#8217;ve got a place in the argument list that&#8217;s scoped with the method body. And you&#8217;ve got a deep desire to mock people who pass default values.</p>
<p>BOOM!</p>
<pre><span class="keyword">def </span><span class="method">greet_world</span><span class="punct">(</span><span class="ident">salutation</span><span class="punct">=(</span><span class="ident">salutation_not_given</span><span class="punct">=&#8217;</span><span class="string">Hello</span><span class="punct">&#8216;))</span>
  <span class="keyword">if</span> <span class="ident">salutation</span> <span class="punct">==</span> <span class="punct">&#8216;</span><span class="string">Hello</span><span class="punct">&#8216;</span> <span class="keyword">and</span> <span class="keyword">not</span> <span class="ident">salutation_not_given</span>
    <span class="punct">&#8220;</span><span class="string">YOU HAVE NO IMAGINATION!</span><span class="punct">&#8220;</span>
  <span class="keyword">else</span>
    <span class="punct">&#8220;</span><span class="string"><span class="expr">#{salutation}</span> World!</span><span class="punct">&#8220;</span>
  <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="ident">greet_world</span><span class="punct">(&#8217;</span><span class="string">Hello</span><span class="punct">&#8216;)</span>  <span class="comment"># =&gt; &#8220;YOU HAVE NO IMAGINATION!&#8221;</span>
<span class="ident">greet_world</span>           <span class="comment"># =&gt; &#8220;Hello World!&#8221;</span>
<span class="ident">greet_world</span><span class="punct">(&#8217;</span><span class="string">Hi</span><span class="punct">&#8216;)</span>     <span class="comment"># =&gt; &#8220;Hi World!&#8221;</span></pre>
<p><code>salutation_not_given</code> is nil if you&#8217;ve given a value. It&#8217;s one of those things.</p>
<p>ANYWAY. Use that at your own peril.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/64/evil-knowing-when-a-method-got-passed-the-default-value/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MountainWest RubyConf videos (including my lightning talk on GuessMethod)</title>
		<link>http://ruby.tie-rack.org/63/mountainwest-rubyconf-videos-including-my-lightning-talk-on-guessmethod/</link>
		<comments>http://ruby.tie-rack.org/63/mountainwest-rubyconf-videos-including-my-lightning-talk-on-guessmethod/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 23:09:20 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[GuessMethod]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/63/mountainwest-rubyconf-videos-including-my-lightning-talk-on-guessmethod/</guid>
		<description><![CDATA[Confreaks have started putting up more video from MountainWest RubyConf, and today saw the addition of the lightning talks from day one. I&#8217;d like to draw particular attention to the second entry of this list of who talked when about what:

Binary Lottery in Shoes - Mike Moore - 0:10
GuessMethod - Chris Shea (me!) - 4:35
ActiveScaffold [...]]]></description>
			<content:encoded><![CDATA[<p>Confreaks have started putting up more <a href="http://mwrc2008.confreaks.com/">video from MountainWest RubyConf</a>, and today saw the addition of the <a href="http://mwrc2008.confreaks.com/06lightning.html">lightning talks from day one</a>. I&#8217;d like to draw particular attention to the second entry of this list of who talked when about what:</p>
<ol>
<li>Binary Lottery in Shoes - Mike Moore - 0:10</li>
<li>GuessMethod - Chris Shea (me!) - 4:35</li>
<li>ActiveScaffold - Ed Moss - 8:51</li>
<li>Migration Concordance - Josh Susser - 12:39</li>
<li>Framework Components - Jeremy McAnally - 18:05</li>
<li>Systems Building Systems with Puppet - Andrew Schafer - 20:43</li>
<li>Multi-image Uploading with SWFUpload and Rails - David South - 27:14</li>
<li>RubyAmp: Ruby Dev in TextMate Amplified - Tim Harper - 31:55</li>
<li>Build a GUI App in 5 Minutes - David Koontz - 37:46</li>
</ol>
<p>Lots of interesting stuff in there. And I encourage everyone to look at other videos from the conference. It was great.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/63/mountainwest-rubyconf-videos-including-my-lightning-talk-on-guessmethod/feed/</wfw:commentRss>
		</item>
		<item>
		<title>GuessMethod 0.2.1</title>
		<link>http://ruby.tie-rack.org/61/guessmethod-021/</link>
		<comments>http://ruby.tie-rack.org/61/guessmethod-021/#comments</comments>
		<pubDate>Sun, 30 Mar 2008 04:31:26 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[GuessMethod]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/61/guessmethod-021/</guid>
		<description><![CDATA[I gave a lightning talk about GuessMethod at MountainWest RubyConf, and then I looked at the code again. I figured it could use a little cleaning, so I took my steel wool to it.
I fixed up some of the documentation and the website, made the specs run again (they always passed, they just stopped running [...]]]></description>
			<content:encoded><![CDATA[<p>I gave a lightning talk about <a href="http://guessmethod.rubyforge.org/">GuessMethod</a> at MountainWest RubyConf, and then I looked at the code again. I figured it could use a little cleaning, so I took my steel wool to it.</p>
<p>I fixed up some of the documentation and the website, made the specs run again (they always passed, they just stopped running at some point&#8230; rspec change?), and rewrote the code that finds close matches of missing methods and constant names. And that last change (based on a naïve benchmark) doubled its speed (of course, iterating over an array once instead of twice will sort of do that).</p>
<p>So, if you need aggressive spell-checking in your irb or script/console sessions check it out:</p>
<pre class="terminal">001:0&gt; Strin.tos
<span style="color: #ff0000">attention:</span> replacing non-existant constant <span style="color: #00ffff">Strin</span> with <span style="color: #00ffff">String</span> for <span style="color: #00ff00">Object</span>
<span style="color: #ff0000">attention:</span> sending <span style="color: #00ffff">to_s</span> instead of <span style="color: #00ffff">tos</span> to <span style="color: #00ff00">String:Class</span>
“String”</pre>
<p>Or, if you sometimes can&#8217;t be bothered to correctly remember rake task names (and <code>rake -T</code> takes too long), use grake!</p>
<pre class="terminal">mvb:~/hot-rails-app cms$ grake db:migrat
(in /Users/cms/hot-rails-app)
<span style="color: #ff0000">attention</span>: invoking task <span style="color: #00ffff">db:migrate</span> instead of <span style="color: #00ffff">db:migrat</span>
&#8230;</pre>
<p>It might show up in Giles Bowkett&#8217;s <a href="http://utilitybelt.rubyforge.org/">utility_belt</a> at some point, in case you&#8217;re into that and you can&#8217;t be bothered with this:</p>
<pre class="terminal">sudo gem install guessmethod</pre>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/61/guessmethod-021/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Salt Lake City</title>
		<link>http://ruby.tie-rack.org/59/salt-lake-city/</link>
		<comments>http://ruby.tie-rack.org/59/salt-lake-city/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 00:52:49 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/59/salt-lake-city/</guid>
		<description><![CDATA[I&#8217;m in Salt Lake City for MountainWest RubyConf. Say hello. I&#8217;ll be wearing an Orioles cap and two hooded sweatshirts the whole time.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m in Salt Lake City for <a href="http://mtnwestrubyconf.org/">MountainWest RubyConf</a>. Say hello. I&#8217;ll be wearing an Orioles cap and two hooded sweatshirts the whole time.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/59/salt-lake-city/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Speeding up gem on OS X Leopard</title>
		<link>http://ruby.tie-rack.org/58/speeding-up-gem-on-os-x-leopard/</link>
		<comments>http://ruby.tie-rack.org/58/speeding-up-gem-on-os-x-leopard/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 19:11:53 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[os x]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/58/speeding-up-gem-on-os-x-leopard/</guid>
		<description><![CDATA[Update: gem has changed for the better, and this is no longer necessary. Go ahead and gem update --system.
After upgrading to Leopard from Tiger, I noticed that certain network operations for some processes suddenly took forever. Most noticeably, when gem would update metadata. And I&#8217;m not the only one.
It&#8217;s not so bad anymore, and all [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update</strong>: gem has changed for the better, and this is no longer necessary. Go ahead and <code>gem update --system</code>.</p>
<p>After upgrading to Leopard from Tiger, I noticed that certain network operations for some processes suddenly took forever. Most noticeably, when <code>gem </code>would update metadata. And I&#8217;m <a href="http://flickr.com/photos/aaronp/2345227545/">not the only one</a>.</p>
<p>It&#8217;s not so bad anymore, and all I had to do was grab something from the standard library.</p>
<p>In <code>gem</code> (mine&#8217;s at <code>/usr/local/bin/gem</code>), add this line before any other code:</p>
<pre>require 'resolv-replace'</pre>
<p>Done. Seriously. <code>gem</code> will work much faster. Not as fast as you remember from Tiger, but getting there.</p>
<p>Discussions I&#8217;ve had suggest that Leopard isn&#8217;t caching DNS requests from certain processes, and letting Ruby handle that itself gets around this.</p>
<p><strong>Update</strong>: Here&#8217;s the current state of my /usr/local/bin/gem:</p>
<pre class="terminal">#!/usr/local/bin/ruby
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++

require 'resolv-replace'

require 'rubygems'
require 'rubygems/gem_runner'

required_version = Gem::Requirement.new "&gt;= 1.8.2"

unless required_version.satisfied_by? Gem::Version.new(RUBY_VERSION) then
  abort "Expected Ruby Version #{required_version}, was #{RUBY_VERSION}"
end

# We need to preserve the original ARGV to use for passing gem options
# to source gems.  If there is a -- in the line, strip all options after
# it...its for the source building process.
args = !ARGV.include?("--") ? ARGV.clone : ARGV[0...ARGV.index("--")]

Gem::GemRunner.new.run args</pre>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/58/speeding-up-gem-on-os-x-leopard/feed/</wfw:commentRss>
		</item>
		<item>
		<title>You might be able to stop trying to call methods on nil</title>
		<link>http://ruby.tie-rack.org/54/you-might-be-able-to-stop-trying-to-call-methods-on-nil/</link>
		<comments>http://ruby.tie-rack.org/54/you-might-be-able-to-stop-trying-to-call-methods-on-nil/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 17:14:53 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/54/you-might-be-able-to-stop-trying-to-call-methods-on-nil/</guid>
		<description><![CDATA[Correct me if I&#8217;m mistaken. Please.
The people behind andand, try, do_or_do_not, NilClass#method_missing, SafeNil, maybe, etc. seem to be trying to beautifully solve the problem of calling methods on objects that might be nil and not having Ruby complain about it. They might be chaining methods a lot or, when they check if an object is [...]]]></description>
			<content:encoded><![CDATA[<p>Correct me if I&#8217;m mistaken. Please.</p>
<p>The people behind <a href="http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html">andand</a>, <a href="http://ozmm.org/posts/try.html">try</a>, <a href="http://www.bencurtis.com/archives/2008/03/do-or-do-not/">do_or_do_not</a>, <a href="http://blog.rubyenrails.nl/articles/2008/02/29/our-daily-method-18-nilclass-method_missing">NilClass#method_missing</a>, <a href="http://coderrr.wordpress.com/2007/09/15/the-ternary-destroyer/">SafeNil</a>, <a href="http://blog.pretheory.com/arch/2008/02/the_maybe_monad_in_ruby.php">maybe</a>, etc. seem to be trying to beautifully solve the problem of calling methods on objects that might be nil and not having Ruby complain about it. They might be chaining methods a lot or, when they check if an object is nil, they use the ternary conditional and (rightly) think it&#8217;s ugly.</p>
<p>As for these ternary conditional users, you can do this: <code>@person.name if @person</code> instead of <code>@person ? @person.name : nil</code>. It does what you need, looks great, and is highly readable.</p>
<p>Of course, that doesn&#8217;t help you if you&#8217;re chaining methods.</p>
<p>But (and this is what&#8217;s been on my mind), I suspect that all of these solutions value terseness over readability and flexibility. It&#8217;s a trade off I&#8217;m willing to make sometimes, but I think it&#8217;s unnecessary here.</p>
<p>For example, <a href="http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html">Reg Braithwaite&#8217;s example</a>:</p>
<p><code>@phone = Location.find(:first, ...elided... ).andand.phone</code></p>
<p>We&#8217;re trying to get the phone number of a location, but maybe we won&#8217;t find the location. Seems normal on the surface. But these are the questions that run through my mind looking at this:</p>
<ol>
<li>All you need is the phone number?</li>
<li>You&#8217;re sure you don&#8217;t need anything else from the Location object? (address, url, hours, etc.)</li>
<li>It doesn&#8217;t mean anything if the location isn&#8217;t found?</li>
</ol>
<p>I find it hard to believe that the answer to all these questions is &#8220;Yes&#8221;. I know this is example code, but it must be inspired from something, right?</p>
<p>I&#8217;d bet that instead of <code>@phone = Location.find(:first, ...elided... ).andand.phone</code>, later on you&#8217;d really wish you had this:</p>
<p><code>@location = Location.find(:first, ...elided... )</code></p>
<p>Maybe you&#8217;d discover your view (Reg&#8217;s example is ActiveResource-related, I&#8217;m running with that) needs more than just the phone number (who are you calling?). Or maybe you&#8217;d discover that if the location isn&#8217;t found, that that <em>means</em> something. Maybe you need to alert the user (<code>unless @location ...</code>).</p>
<p>It could be that I&#8217;m more hung up on this idea of meaning. <a href="http://blog.pretheory.com/arch/2008/02/the_maybe_monad_in_ruby.php">The example for Ben&#8217;s maybe</a> is:</p>
<p><code>if(customer &amp;&amp; customer.order &amp;&amp; customer.order.id==newest_customer_id)</code></p>
<p>I look at that and think: if there&#8217;s no customer, that means one thing; if there&#8217;s no order for the customer, that means something else; if the customer&#8217;s order&#8217;s id isn&#8217;t newest_customer_id, that means something else entirely; and if all these things are different, maybe they should be handled separately.</p>
<p>If you think about it that way, there&#8217;s no need to fiddle with nil or add methods to Object.</p>
<p>(I completely understand that this might just be an aesthetic difference, or that sometimes this apparent loss of flexibility is desirable, or that I might be wrong. Please narrow it down for me in the comments. Thanks.)</p>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/54/you-might-be-able-to-stop-trying-to-call-methods-on-nil/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A better try &#8212; chaining methods and nil</title>
		<link>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/</link>
		<comments>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 22:20:45 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[metaprogramming]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/</guid>
		<description><![CDATA[Of #try, #andand, and #do_or_do_not, the one I like best (and I don&#8217;t even like any of them that much, but that&#8217;s my next post) is #try.
What they are all basically for is chaining method calls when one of methods might return nil, resulting in a NoMethodError. Here&#8217;s Chris Wanstrath&#8217;s implementation:
class Object
  ##
  [...]]]></description>
			<content:encoded><![CDATA[<p>Of <a href="http://ozmm.org/posts/try.html">#try</a>, <a href="http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html">#andand</a>, and <a href="http://www.bencurtis.com/archives/2008/03/do-or-do-not/">#do_or_do_not</a>, the one I like best (and I don&#8217;t even like any of them that much, but that&#8217;s my next post) is #try.</p>
<p>What they are all basically for is chaining method calls when one of methods might return nil, resulting in a NoMethodError. Here&#8217;s Chris Wanstrath&#8217;s implementation:</p>
<pre><span class="keyword">class </span><span class="class">Object</span>
  <span class="comment">##</span>
  <span class="comment">#   @person ? @person.name : nil</span>
  <span class="comment"># vs</span>
  <span class="comment">#   @person.try(:name)</span>
  <span class="keyword">def </span><span class="method">try</span><span class="punct">(</span><span class="ident">method</span><span class="punct">)</span>
    <span class="ident">send</span> <span class="ident">method</span> <span class="keyword">if</span> <span class="ident">respond_to?</span> <span class="ident">method</span>
  <span class="keyword">end</span>
<span class="keyword">end</span></pre>
<p>It&#8217;s almost right!</p>
<p>First of all, I&#8217;d argue that if #try is trying to be better than something, it had better be better than <code>@person.name if @person</code>. That looks and reads so nicely I can scarcely see why it needs replacing. <code>@person.name if @person</code> is so obviously better than <code>@person ? @person.name : nil</code> that I hope people are using that before they start thinking about monkey patching Object (which I hear is destroying Ruby). Anyway, I&#8217;d fix the documentation first.</p>
<p>But I think the biggest problem with #try is that it&#8217;s not doing what it says it&#8217;s doing. It&#8217;s <em>not</em> a replacement for <code>@person ? @person.name : nil</code>. That&#8217;s certainly not what the method is doing. Here&#8217;s what I&#8217;d do:</p>
<pre><span class="keyword">class </span><span class="class">Object</span>
  <span class="comment">##</span>
  <span class="comment">#   @person.name if @person</span>
  <span class="comment"># vs</span>
  <span class="comment">#   @person.try(:name)</span>
  <span class="keyword">def </span><span class="method">try</span><span class="punct">(</span><span class="ident">method</span><span class="punct">)</span>
    <span class="constant">self</span><span class="punct">.</span><span class="ident">send</span><span class="punct">(</span><span class="ident">method</span><span class="punct">)</span> <span class="keyword">if</span> <span class="constant">self</span>
  <span class="keyword">end</span>
<span class="keyword">end</span></pre>
<p>See? Much clearer; it does what it says. Using respond_to? means that the original #try is about something else; it&#8217;s about whether or not an object responds to a message. This #try is about sending a message to an object unless it&#8217;s nil or false, which is closer to what the original use case was&#8230; right?</p>
<p>But then it occurred to me that the issue is nil, not nil or false. The problem isn&#8217;t getting false in the middle of method chains, it&#8217;s nil. So maybe what #try should really be is this:</p>
<pre><span class="keyword">class </span><span class="class">Object</span>
  <span class="comment">##</span>
  <span class="comment">#   @person.name unless @person.nil?</span>
  <span class="comment"># vs</span>
  <span class="comment">#   @person.try(:name)</span>
  <span class="keyword">def </span><span class="method">try</span><span class="punct">(</span><span class="ident">method</span><span class="punct">)</span>
    <span class="constant">self</span><span class="punct">.</span><span class="ident">send</span><span class="punct">(</span><span class="ident">method</span><span class="punct">)</span> <span class="keyword">unless</span> <span class="constant">self</span><span class="punct">.</span><span class="ident">nil?</span>
  <span class="keyword">end</span>
<span class="keyword">end</span></pre>
<p>I might even use that.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Boy, what ISN&#8217;T destroying Ruby these days?</title>
		<link>http://ruby.tie-rack.org/52/boy-what-isnt-destroying-ruby-these-days/</link>
		<comments>http://ruby.tie-rack.org/52/boy-what-isnt-destroying-ruby-these-days/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 02:37:18 +0000</pubDate>
		<dc:creator>Chris Shea</dc:creator>
		
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ruby.tie-rack.org/52/boy-what-isnt-destroying-ruby-these-days/</guid>
		<description><![CDATA[
Monkey Patching
Slowness
Thread#raise, Thread#kill, timeout.rb, and net/protocol.rb
Rails/the community/ThoughtWorks/&#8230;

Cleverness
No One True Way
MRI/YARV/Rubinius/JRuby/XRuby

What else?
(Note: Charles Oliver Nutter is helping when he tells us that Thread#raise, Thread#kill, and timeout.rb are broken.)
]]></description>
			<content:encoded><![CDATA[<ul>
<li><a href="http://avdi.org/devblog/2008/02/23/why-monkeypatching-is-destroying-ruby/">Monkey Patching</a></li>
<li><a href="http://whynotwiki.com/Ruby_is_slow">Slowness</a></li>
<li><a href="http://headius.blogspot.com/2008/02/rubys-threadraise-threadkill-timeoutrb.html">Thread#raise, Thread#kill, timeout.rb, and net/protocol.rb</a></li>
<li><a href="http://zedshaw.com/rants/rails_is_a_ghetto.html">Rails/the community/ThoughtWorks/&#8230;<br />
</a></li>
<li><a href="http://fleshy.org.nz/yum/2007/05/02/observation-code-cleverness-bad/">Cleverness</a></li>
<li><a href="http://www.amazon.com/review/R1B2QTA8Z395QN/ref=cm_cr_rdp_perm">No One True Way</a></li>
<li><a href="http://blog.lostlake.org/index.php?/archives/11-The-Impending-Ruby-Fracture.html">MRI/YARV/Rubinius/JRuby/XRuby</a></li>
</ul>
<p>What else?</p>
<p>(Note: Charles Oliver Nutter is helping when he tells us that Thread#raise, Thread#kill, and timeout.rb are broken.)</p>
]]></content:encoded>
			<wfw:commentRss>http://ruby.tie-rack.org/52/boy-what-isnt-destroying-ruby-these-days/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
