Consider the Ruby Foo::Bar class.
The convention is that the Foo namespace is a module, but it can just as easily be a class:
module Foo; class Bar; end; end
Versus:
class Foo; class Bar; end; end
In the second case, Bar not an inner class of Foo , it is just another constant defined on the Foo singleton. The superclass is Object in both cases, and it includes only the Kernel module. The goals of their ancestors are identical.
So, in addition to the operations that you could do with Foo depending on your class (instance, if class, extension / inclusion, if module), does the nature of the namespace affect Bar ? Are there any good reasons to choose one of several namespaces over another?
The only weird things I see you can do: Foo::Bar.new.extend Foo and Foo.new.class::Bar respectively.
My own most common use of a class defined in a class would be an auxiliary structure / class, which should only be used inside the class:
class Foo Bar = Struct.new(:something) do def digest puts "eating #{something}" end end def eat(something) Bar.new(something).digest end end
The closest I found to this discussion is in Using the vs Module class to package code in Ruby .
source share