From: Martin DeMello Date: 2003-08-10T14:13:39+09:00 Subject: Re: Ruby and OOP-design (question of an old "procedural person" ;) Kent Dahl wrote: > > Consider inheritance: You should comply with the interface of the > superclass for substitutability. However, you may augment and extend it > as long as it is still possible to substitute a parent object with a > child object. > > nonzero? just does this using core design decisions of Ruby (nil and > false are only false values) instead of classes. That is a good point, yes. >> Using the value of self feels >> like relying on an undocumented side effect. > > Now I know Ruby is easy to read, but we can't have _all_ the > documentation be Ruby source code. :-) > http://www.rubycentral.com/book/ref_c_numeric.html#Numeric.nonzero_qm I know it's documented - that's why I said 'feels' :) >> (I feel the same way about >> ! methods returning nil rather than self when they haven't made a >> change). > > Are you saying the usage of ? and ! should be restricted to only the two > protocols/types/interfaces? I.e. > ? => result either true or false and Yes > ! => self if modified, nil if unmodified ! => self always. Frinstance, why can't we have something like Module Modified def modified? true end end Module UnModified def modified? false end end def method! if changed? self.extend(Modified) else self.extend(UnModified) end self end > That would be a huge blow against the readability potential of Ruby > code, IMHO: > > Real life: > - "Have you found the answer to life, the universe and everything?" > - "Yeah, it's 42." > Ruby code: > adams.found_answer? #=> 42 adams.found_answer? #=> true, false adams.answer #=> 42, nil Far more readable, IMO - in fact, it was the unintutive reading of a.nonzero? that took me aback in the first place. martin