From: Robert Klemme Date: 2005-05-06T19:14:25+09:00 Subject: Re: RCR 303: nil should accept missing methods and return nil "John Carter" schrieb im Newsbeitrag news:Pine.LNX.4.61.0505061350130.28698@parore.tait.co.nz... >A very simple and generic way of improving the reliability of Ruby > programs is to implement the NullObject pattern by allowing nil to > accept all and every method instead of throwing a NoMethodError. > > Not only does this simplify ruby programs considerably, it also > changes certain crash bugs silently into correct programs. I'm sorry, is this some kind of hoax? A program will not magically be correct simply because an error is hidden. Think of all the subtle bugs this would introduce, for example: you create an array with a fixed number of elements and forget to give the default value 55. Now math would work, but the results would be wrong - and you might not even catch this. The article at http://www.smalltalkchronicles.net/edition2-1/null_object_pattern.htm that you refer to in the RCR states, that it's a bad idea to introduce this change in Smalltalk because it would break too much code. The situation in Ruby is similar. > If you were to investigate most cases where an if statement checks to > see if a value is non-nil, you will find that the else clause is > empty. ie. Does nothing. But that would only work in cases like unless x.nil? x.do_something end i.e. where the block invokes methods on nil. This could be safely transferred to x.do_something with your approach. But other cases cannot. Regards robert