Hakan Ensari
Hakan Ensari

Reputation: 1979

How do I include a module in a namespaced class?

I am having trouble including a module in a namespaced class. The example below throws the error uninitialized constant Bar::Foo::Baz (NameError). What basic piece of Ruby knowledge am I missing here?

module Foo
  module Baz
    def hello
      puts 'hello'
    end
  end
end

module Bar
  class Foo
    include Foo::Baz
  end
end

foo = Bar::Foo.new

Upvotes: 6

Views: 204

Answers (2)

molf
molf

Reputation: 74945

Use :: to force the lookup to the top level only:

module Bar
  class Foo
    include ::Foo::Baz
  end
end

Upvotes: 7

vladr
vladr

Reputation: 66681

include ::Foo::Baz

Upvotes: 0

Related Questions