--001636c5b4d8b79d06049a54bd0e
Content-Type: text/plain; charset=UTF-8

On Thu, Jan 20, 2011 at 11:56 PM, Charles Oliver Nutter <headius / headius.com
> wrote:

> On Thu, Jan 20, 2011 at 7:01 PM, Jim Weirich <jim.weirich / gmail.com>
> wrote:
> >
> > On Jan 20, 2011, at 7:52 PM, Charles Oliver Nutter wrote:
> >
> >> This also short-circuits further calls...but it's pretty ugly :)
> >
> > Also doesn't work if a method returns a type other than self.class.
>
> Well, you'd mix this into Object or something if you wanted it globally...
>

Really, this is a deeper question of programming philosophy. The problem
isn't the lack of a ?., &., #try, etc. method. The problem is that nil does
not respond to arbitrary messages in Ruby. Contrast this with Objective-C,
where NIL responds to arbitrary messages with NIL.

You can quite easily replicate Objective-C behavior in Ruby with the
following:

irb(main):001:0> def maybe_a_string
irb(main):002:1>   ["test", nil][rand(2)]
irb(main):003:1> end
nil
irb(main):004:0> maybe_a_string.slice(1,2)
"es"
irb(main):005:0> maybe_a_string.slice(1,2)
NoMethodError: undefined method `slice' for nil:NilClass
from (irb):5
from :0
irb(main):006:0> class<<nil
irb(main):007:1>   def method_missing(meth, *args)
irb(main):008:2>     nil
irb(main):009:2>   end
irb(main):010:1> end
nil
irb(main):011:0> maybe_a_string.slice(1,2)
"es"
irb(main):012:0> maybe_a_string.slice(1,2)
nil

--001636c5b4d8b79d06049a54bd0e
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div class="gmail_quote">On Thu, Jan 20, 2011 at 11:56 PM, Charles Oliverutter <span dir="ltr">&lt;headius / headius.com&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Thu, Jan 20, 2011 at 7:01 PM, Jim Weirich &lt;jim.weirich / gmail.com&gt; wrote:<br>
&gt;<br>
&gt; On Jan 20, 2011, at 7:52 PM, Charles Oliver Nutter wrote:<br>
&gt;<br>
&gt;&gt; This also short-circuits further calls...but it&#39;s pretty ugly :)<br>
&gt;<br>
&gt; Also doesn&#39;t work if a method returns a type other than self.class.<br>
<br>
</div>Well, you&#39;d mix this into Object or something if you wanted it globally...<br></blockquote><div><br></div><div>Really, this is a deeper question of programming philosophy. The problem isn&#39;t the lack of a ?., &amp;., #try, etc. method. The problem is that nil does not respond to arbitrary messages in Ruby. Contrast this with Objective-C, where NIL responds to arbitrary messages with NIL.</div>
<div><br></div><div>You can quite easily replicate Objective-C behavior in Ruby with the following:</div><div><br></div><div>irb(main):001:0&gt; def maybe_a_string</div><div>irb(main):002:1&gt;  [&quot;test&quot;, nil][rand(2)]</div>
<div>irb(main):003:1&gt; end</div><div>=&gt; nil</div><div>irb(main):004:0&gt; maybe_a_string.slice(1,2)</div><div>=&gt; &quot;es&quot;</div><div>irb(main):005:0&gt; maybe_a_string.slice(1,2)</div><div>NoMethodError: undefined method `slice&#39; for nil:NilClass</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>from (irb):5</div><div><span class="Apple-tab-span" style="white-space:pre">/span>from :0</div><div>irb(main):006:0&gt; class&lt;&lt;nil</div><div>irb(main):007:1&gt;  def method_missing(meth, *args)</div>
<div>irb(main):008:2&gt;   nil</div><div>irb(main):009:2&gt;  end</div><div>irb(main):010:1&gt; end</div><div>=&gt; nil</div><div>irb(main):011:0&gt; maybe_a_string.slice(1,2)</div><div>=&gt; &quot;es&quot;</div><div>irb(main):012:0&gt; maybe_a_string.slice(1,2)</div>
<div>=&gt; nil</div><div></div></div>

--001636c5b4d8b79d06049a54bd0e--