Given that module Foo is defined elsewhere what is the difference between the following 2 code snippets?
class Foo::Bar ... end
module Foo class Bar ... end end
It is actually quite simple. The following code:
# foo.rb module Foo BAR = 123 end module Foo class A puts BAR end end class Foo::B puts BAR end
123 foo.rb:13:in `': uninitialized constant Foo::B::BAR (NameError) from foo.rb:12:in `
Simply put, while A has access to the insides of the module's Foo namespace, class B is defined outside of this namespace, only the result of the definition is put as a constant inside Foo.
I most frequently stumble on this when I want to define some common constants in the parent module, just like in the example above. Given that and the fact that the 2nd form doesn't actually require the module to be defined, the 2nd form is probably better and is a safer bet in most cases.