title: Ruby namespacing with a class vs. module?
body: <p>Consider the Ruby class <code>Foo::Bar</code>.</p>
  
  <p>The convention is for the 'Foo' namespace to be a module, but it could just as easily be a class:</p>
  
  <pre><code>module Foo; class Bar; end; end
  </code></pre>
  
  <p>Versus:</p>
  
  <pre><code>class Foo; class Bar; end; end
  </code></pre>
  
  <p>In the second case, <code>Bar</code> is not an inner class of <code>Foo</code>, it's just another constant defined on the singleton of <code>Foo</code>. The superclass is Object in both cases and they only include the Kernel module. Their ancestor chains are identical.</p>
  
  <p>So, besides operations you could make with <code>Foo</code> depending on its class (instantiate if a class, extend/include if a module), does the nature of the namespace have any effect on <code>Bar</code>?  Are there compelling reasons to choose one many of name-spacing over another?</p>
  
  <p>The only sort of weird things I see you can do are <code>Foo::Bar.new.extend Foo</code> and <code>Foo.new.class::Bar</code> respectively.</p>
  
  <p>My own most-common use of a class defined in a class would be a helper struct/class that is meant to be only used internally by the class:</p>
  
  <pre><code>class Foo
    Bar = Struct.new(:something) do
      def digest
        puts "eating #{something}"
      end
    end
    def eat(something)
      Bar.new(something).digest
    end
  end
  </code></pre>
  
  <p>The closest I found to this discussion is in "<a href="https://stackoverflow.com/questions/8496803/using-class-vs-module-for-packaging-code-in-ruby">Using Class vs Module for packaging code in Ruby</a>".</p>