From: Kent Dahl Date: 2003-02-13T04:25:05+09:00 Subject: Re: inheriting from base classes Gavin Sinclair wrote: > I've stated enough times that inheriting from generic classes without > preserving that generality is bad (IMO), e.g. > > class OrderedHash < Hash # good > > class AssignmentResults < Hash # bad > > And one reason is that you are not putting enough effort into what > methods are available to the users of your class. > > So my previous post about the badness of too many methods is > consistent with my point of view on this whole thread. No such thing as too many methods, just a thing as too many accessible methods. :-) I can't think it is too difficult to make every method protected when subclassing and then opening only up those methods that are actually needed by the outside world. class Module def major_inheritance_lockdown puts "Lock 'er down tight!" superclass.instance_methods.each{|meth| protected meth } end end class AssignmentResults < Hash major_inheritance_lockdown public :each, :[] #etc end a = AssignmentResults.new a[5]=2 #=>toomanymethods.rb:17: protected method `[]=' called for {}:AssignmentResults (NameError) I'd plug all the holes and loose ends, but I've got a pizza is in the oven. [insert DuckTyping argument that goes pro this kind of madness here] In practice, I use SimpleDelegator a lot in this kind of situations, but I'm somewhat worried about performance with it. -- (\[ Kent Dahl ]/)_ _~_ __[ http://www.stud.ntnu.no/~kentda/ ]___/~ ))\_student_/(( \__d L b__/ NTNU - graduate engineering - 5. year ) ( \__\_�|�_/__/ ) _)Industrial economics and technological management( \____/_�_\____/ (____engineering.discipline_=_Computer::Technology___)