Ruby

Ruby

Made by DeepSource

Inconsistent class/module namespace nesting RB-ST1015

Anti-pattern
Minor

Namespaced classes and modules should be defined (and reopened) in a consistent way throughout the project. Using the scope resolution operator can lead to surprising constant lookups due to Ruby’s lexical scoping, which depends on the module nesting at the point of definition.

By default this issue checks that you have explicitly nested your classes and modules. However if you have a .rubocop.yml which sets EnforcedStyle: compact for Style/ClassAndModuleChildren cop we will respect that configuration and give you issue results accordingly.

Bad practice

module Utilities
  class Queue
  end
end

class Utilities::Store
  Module.nesting # => [Utilities::Store]

  def initialize
    # Refers to the top level ::Queue class because Utilities isn't in the
    # current nesting chain.
    @queue = Queue.new
  end
end

Recommended

module Utilities
  class Queue
  end
end

module Utilities
  class WaitingList
    Module.nesting # => [Utilities::WaitingList, Utilities]

    def initialize
      @queue = Queue.new # Refers to Utilities::Queue
    end
  end
end