2009/5/26 Simon Chiang <simon.a.chiang / gmail.com>:
> I sometimes use nested classes to avoid namespace clutter. =A0The only
> downsides I found are related to inheritance, in particular when you
> split out classes into different files.
>
> =A0[a.rb]
> =A0require 'a/b'
> =A0class A < SuperClass
> =A0end
>
> =A0[a/b.rb]
> =A0class A < SuperClass
> =A0 =A0class B
> =A0 =A0end
> =A0end
>
> In both files you have to have the correct superclass (which can be
> hard during refactoring). =A0There are different ways to construct this,
> however:
>
> =A0[a.rb]
> =A0class A < SuperClass
> =A0 =A0require 'a/b'
> =A0end
>
> =A0[a/b.rb]
> =A0class A::B
> =A0end
>
> 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


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/