--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"><headius / headius.com></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 <jim.weirich / gmail.com> wrote:<br> ><br> > On Jan 20, 2011, at 7:52 PM, Charles Oliver Nutter wrote:<br> ><br> >> This also short-circuits further calls...but it's pretty ugly :)<br> ><br> > Also doesn't work if a method returns a type other than self.class.<br> <br> </div>Well, you'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'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.</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> def maybe_a_string</div><div>irb(main):002:1> ["test", nil][rand(2)]</div> <div>irb(main):003:1> end</div><div>=> nil</div><div>irb(main):004:0> maybe_a_string.slice(1,2)</div><div>=> "es"</div><div>irb(main):005:0> maybe_a_string.slice(1,2)</div><div>NoMethodError: undefined method `slice' 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> class<<nil</div><div>irb(main):007:1> def method_missing(meth, *args)</div> <div>irb(main):008:2> nil</div><div>irb(main):009:2> end</div><div>irb(main):010:1> end</div><div>=> nil</div><div>irb(main):011:0> maybe_a_string.slice(1,2)</div><div>=> "es"</div><div>irb(main):012:0> maybe_a_string.slice(1,2)</div> <div>=> nil</div><div></div></div> --001636c5b4d8b79d06049a54bd0e--