From: pjb@... (Pascal J. Bourguignon) Date: 2009-05-25T20:15:09+09:00 Subject: Re: Do you nest classes inside classes? Mike Stephens writes: > Object Orientation is conceptually about a sea of objects interacting > with each other. They are all however in one sea. > > Earlier in Computer Science, we had stepwise refinement, which was a > tree-like view of application compartmentalisation. High level functions > would be split into lower level consituents and so on. > > I was thinking to model that in Ruby you would like to be able to > contain classes within other classes in a similar tree like format, and > what a shame that Ruby doesn't let you do that. > > But of course it does. However people don't talk about it. Hal Fulton in > his book The Ruby Way asks people to let him know if they can think of a > good use for this feature. > > Do you ever use this? Can you see a reason why it doesn't seem to be > favoured? The problem here is the collusion between two notions: that of namespace and that of class. There's no reason, a-priori, why both notion should match. (In some languages such as Common Lisp, they don't). In Ruby, you could use a module as a pure namespace, and avoid putting classes inside classes: when you have "private" classes you want to keep in a specific namespace, it would be better IMO to put them in a module rather in a class. A good example, would be any abstract data type that needs some internal structure, eg. a Tree with Node. If the nodes are not part of the public API of the tree, there's no reason to make them public, so it would be good to hide them in some namespace specific to the tree. But as I said above, I think it would be better to use a module to package them. -- __Pascal Bourguignon__