From: Robert Klemme Date: 2009-05-27T16:44:38+09:00 Subject: Re: Do you nest classes inside classes? 2009/5/26 Simon Chiang : > I sometimes use nested classes to avoid namespace clutter.  The only > downsides I found are related to inheritance, in particular when you > split out classes into different files. > >  [a.rb] >  require 'a/b' >  class A < SuperClass >  end > >  [a/b.rb] >  class A < SuperClass >    class B >    end >  end > > In both files you have to have the correct superclass (which can be > hard during refactoring).  There are different ways to construct this, > however: > >  [a.rb] >  class A < SuperClass >    require 'a/b' >  end > >  [a/b.rb] >  class A::B >  end > > The downside of this is that 'a/b.rb' can't be required on it's own. IMHO this is not a problem of inheritance but of clear priority: class A should be defined in one primary file. All other files that *modify* it (i.e. by adding method or whatnot) should require that primary file and reopen the class to do whatever they need. So, basically the layout should look like this: [a.rb] class A < SuperClass def method_of_A_which_does_not_use_B end end [a/b.rb] require 'a' class A class B end def method_of_A_which_uses_B end end Actually, if you use A only for namespace handling then a module would probably a better thing to have here. Other than that, if A's implementation needs A::B to work, then splitting both out into two files does not make much sense. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/