nourdine
nourdine

Reputation: 7597

How do you manage naming clashes generated when importing different namespaces in ruby?

Lets' say I have 2 modules:

# a/foo.rb
module A
   class Foo
   end
end

and

# b/foo.rb
module B
   class Foo
   end
end

As you can see they both expose a Foo class. Now, in my main file I do:

load "./a/foo.rb"
load "./b/foo.rb"

include A
include B

i = Foo.new() // this is probably B::Foo isn't it?

How can I differentiate the 2 Foo classes? Is there any aliasing facility in the language that help me preventing the overlapping between the two Foos?

Thanks

Upvotes: 1

Views: 39

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

this is probably B::Foo isn't it?

Yes, it is.

Just use the namespace resolution operator (::) to refer the right constant:

  • A::Foo.new #=> A::Foo:0x007fb9e5b8caa8

  • B::Foo.new #=> B::Foo:0x007fb9e5b549f0

Upvotes: 3

Related Questions