What's the difference between these Ruby namespace conventions?

后端 未结 2 1380
庸人自扰
庸人自扰 2020-12-12 19:40

So Module can be used in Ruby to provide namespacing in addition to mixins, as so:

module SomeNamespace
  class Animal

  end
end

animal = Some         


        
2条回答
  •  庸人自扰
    2020-12-12 20:08

    The only difference between the two approaches is that the second one will throw uninitialized constant Object::SomeNamespace if the namespace hasn't previously been declared.

    When declared in a single file, I would opt for the first one because you don't have to repeat SomeNamespace.

    When using multiple files I also use the second one, to avoid running into the following problem:

    # in a.rb
    require 'b'
    
    module SomeNamespace
      def self.animal
        Animal.new
      end
    end
    
    # in b.rb
    class SomeNamespace::Animal
    
    end
    
    # irb
    require 'a' # explodes with the uninitialized constant error
    

    This example may be contrived, but it's easy to trigger it if your code base is a little bit bigger. I usually use the explicit way (your first one) to avoid this.

    One thing that may be helpful when using the second form is that it will detect typos in the namespace.

    There doesn't seem to be an established way to create namespaces, Devise for example mixes both approaches: first one, second one.

提交回复
热议问题