<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Aaron Feng: Learning from io</title>
    <link>http://www.aaronfeng.com/articles/2008/09/24/learning-from-io</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Adventures in software development</description>
    <item>
      <title>Learning from io</title>
      <description>&lt;p&gt;&lt;a href="http://www.iolanguage.com/"&gt;io&lt;/a&gt; is a small, prototype based, dynamic programming language that is inspired by Smalltalk, Self, NewtonScript, Act1, LISP, and Lua.  Need I say more?  It has a very useful feature that any programmer can appreciate: the "?"&lt;/p&gt;

&lt;p&gt;It's very common to want to check the existence of an object before accessing it.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;if offer != nil
  offer.price
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;offer &amp;amp;&amp;amp; offer.price&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It gets even more tedious when you also need to traverse a couple objects deep. &lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;offer &amp;amp;&amp;amp; offer.price &amp;amp;&amp;amp; offer.price.formatted_amount&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Littering the "check" around the code can quickly cloud up the intention of the code.  In io, you can do the following:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;offer ?price
offer ?price ?formatted_amount&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;No more checking!  However, I want to do that in ruby.  An obvious solution is to open up the nil class and re-implement the method_missing to return nil.  This is a very bad idea, since it applies to ALL objects in the system.  It can hide serious errors within the application and make debugging very difficult.  A better solution is to the following:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class Object
  def _?()
    self
  end
end

class NilClass
  def _?()
    SafeNil.new
  end
end

class SafeNil
  def method_missing(*args, &amp;amp;b)
    nil.send(*args, &amp;amp;b) rescue nil
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above code is taken from this &lt;a href="http://hobocentral.net/blog/2007/08/25/quiz/"&gt;post&lt;/a&gt; The post didn't mention io, but I assumed it is inspired by it.  In ruby you can't just use the "?" for your method name, therefore, _ is prepended before "?".Now you can do following without checking the parent object:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;offer._?.price._?.formatted_amount&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;if offer or price is nil, nil is returned.&lt;/p&gt;</description>
      <pubDate>Wed, 24 Sep 2008 23:09:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c3dae810-320e-4500-9f40-e72d50041b26</guid>
      <author>Aaron Feng</author>
      <link>http://www.aaronfeng.com/articles/2008/09/24/learning-from-io</link>
      <category>ruby</category>
      <category>programming</category>
    </item>
    <item>
      <title>"Learning from io" by Leslie P. Polzer</title>
      <description>&lt;p&gt;In Common Lisp you could just add a method specialized on NIL (or write a macro that does this automatically).&lt;/p&gt;

&lt;p&gt;An alternative would be using the MOP for this.&lt;/p&gt;</description>
      <pubDate>Thu, 25 Sep 2008 07:37:54 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:cf1fdb7c-8302-46d3-bb5f-fc970e35e4b1</guid>
      <link>http://www.aaronfeng.com/articles/2008/09/24/learning-from-io#comment-6001</link>
    </item>
    <item>
      <title>"Learning from io" by Jonathan Tran</title>
      <description>&lt;p&gt;So what you're saying is... you implemented the Maybe monad in Ruby.&lt;/p&gt;</description>
      <pubDate>Thu, 25 Sep 2008 01:05:41 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:e2008cb2-c239-4782-8919-6c5fe80db62f</guid>
      <link>http://www.aaronfeng.com/articles/2008/09/24/learning-from-io#comment-5994</link>
    </item>
  </channel>
</rss>
