<?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/"
	>

<channel>
	<title>Cory O&#039;Daniel - These are just words &#187; nil pattern</title>
	<atom:link href="http://coryodaniel.com/index.php/tag/nil-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://coryodaniel.com</link>
	<description>Software development, thoughts, and randomness</description>
	<lastBuildDate>Sat, 31 Jul 2010 00:04:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby Objective-C like Null Pattern</title>
		<link>http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 20:30:51 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[dryness]]></category>
		<category><![CDATA[nil pattern]]></category>
		<category><![CDATA[null pattern]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=431</guid>
		<description><![CDATA[So, I think the objective c null(nil) pattern is pretty cool. I know that lots of people talk about doing it in different ways. Here is my implementation. I'm not keen on having nil catch method missing, because I'm not sure what havoc that may cause in one of the 10million gems I use. I [...]]]></description>
			<content:encoded><![CDATA[<p>So, I think the objective c null(nil) pattern is pretty cool. I know that lots of people <a href="http://www.justskins.com/forums/messages-to-nil-134744.html">talk</a> about doing it in <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/17785">different</a> <a href="http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/">ways</a>. Here is my implementation. I'm not keen on having nil catch method missing, because I'm not sure what havoc that may cause in one of the 10million gems I use. I also like the last link above (which I just found when writing this article) except that I dont want to type "try" a million times.</p>
<p>This is my preferred method when dealing with deeply nested messages. Stick this in your lib.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> rcall<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>method_names<span style="color:#006600; font-weight:bold;">&#41;</span>
    _result = <span style="color:#0000FF; font-weight:bold;">self</span>
&nbsp;
    method_names.<span style="color:#9900CC;">collect</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>mname<span style="color:#006600; font-weight:bold;">|</span> 
      <span style="color:#9966CC; font-weight:bold;">if</span> _result.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>mname<span style="color:#006600; font-weight:bold;">&#41;</span>
        _result = _result.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>mname<span style="color:#006600; font-weight:bold;">&#41;</span>  
      <span style="color:#9966CC; font-weight:bold;">else</span>
        _result = <span style="color:#0000FF; font-weight:bold;">nil</span>
        <span style="color:#9966CC; font-weight:bold;">break</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    _result
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Wanna use it?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#008000; font-style:italic;"># In this example, if an of the messages return nil, i'll get a MethodMissing error and I'll be scorned.</span>
my_object.<span style="color:#9900CC;">first_message</span>.<span style="color:#9900CC;">second_message</span>.<span style="color:#9900CC;">the_message_whos_value_i_want</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># The problem is, I dont care if its nil, I have a default, the method is still missing.</span>
value = my_object.<span style="color:#9900CC;">first_message</span>.<span style="color:#9900CC;">second_message</span>.<span style="color:#9900CC;">the_message_whos_value_i_want</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;Im ok with this being nil&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># If chain_message encounters a nil, it just returns nil and you can use standard language logic to default that value</span>
<span style="color:#008000; font-style:italic;"># You dont have to do a million if-checks.</span>
value = my_object.<span style="color:#9900CC;">rcall</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:first_message</span>, <span style="color:#ff3333; font-weight:bold;">:second_message</span>, <span style="color:#ff3333; font-weight:bold;">:the_message_whos_value_i_want</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;Im ok with this being nil&quot;</span></pre></td></tr></table></div>

<p>Another Example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Some cool way of finding purchases, be it AR, Datamapper, or whatever, just get an object already</span>
purchases = Purchase.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span> arbitrary_finder_logic <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># I need to output the name of who added the product on some arbitrary report no one will read</span>
<span style="color:#008000; font-style:italic;"># yeah, I know. My validators should have made sure the values were there, I'm just making this up.</span>
purchases.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>purchase<span style="color:#006600; font-weight:bold;">|</span>
  buyer = purchase.<span style="color:#9900CC;">rcall</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:purchaser</span>, <span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;John Doe&quot;</span>
  seller = purchase.<span style="color:#9900CC;">rcall</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:product</span>, <span style="color:#ff3333; font-weight:bold;">:listor</span>, <span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;Jack Sales&quot;</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Buyer: #{buyer}, Seller: #{seller}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Yay, Drying up if-else nil checking statements. High Five.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Ruby+Objective-C+like+Null+Pattern+http://bit.ly/d9sGz6" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/&amp;title=Ruby+Objective-C+like+Null+Pattern" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/&amp;t=Ruby+Objective-C+like+Null+Pattern" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/&amp;title=Ruby+Objective-C+like+Null+Pattern" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/&amp;title=Ruby+Objective-C+like+Null+Pattern" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/03/ruby-objective-c-like-null-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
