Eugen Minciu wrote:
> Excerpts from Stefan Rusterholz's message of Thu Jul 19 16:31:01 +0300 
> 2007:
>> While all of this is fine, maybe that could be improved. My RCR would be
>> solution would put the coder in power as to where he cares about getting
>> nil (e.g. he wants an exception if foo is nil, so he just writes
>> foo.bar, if he doesn't want one, he writes foo->bar) and where he just
>> wants to get the nil value back.
>> 
>> Do you think this RCR is worth to make?
>> 
>> Regards
>> Stefan
>> 
> how about this ugly creature?
> 
> <hack>
> class Object
>   alias regular_method_missing method_missing
>   def method_missing(meth)
>     if meth.to_s[0]=='_'[0]
>       return nil
>     else
>       regular_method_missing(meth)
>     end
>   end
> end

Interesting idea :) This one won't work right away I think, but it's a 
starting point.
Btw., you can do ?_ instead of '_'[0]. Syntax would have the benefit of 
optimization, though.

> <proper way>
> begin
>   foo.bar.baz.whatever
> rescue
>   puts "yay it works"
> end

You'd have to rescue NoMethodError, since else you could miss important 
exceptions. For the same reason, single line rescue is disqualified. And 
with a rescue, it is even less readable than working around the nil case 
via multiple lines :-/

Regards
Stefan

-- 
Posted via http://www.ruby-forum.com/.