Problem with Module/Class name resolution

T

Thomas Leitner

Hi,

consider the following:

module A
X = 1
end

p A::X # => 1

module B
include A
end

p B::X # => 1

module A
module Help
p X # => 1
end
end

module B
module Help
p X # => NameError: uninitialized constant B::Help::X
end
end


Why does referencing X from A, B, and A::Help work but not from
B::Help although A is included in B? Is there a work-around to make
this work?

Bye,
Thomas
 
S

SonOfLilit

To expand the knowledge on this weirdness:

irb(main):001:0> module A
irb(main):002:1> module H
irb(main):003:2> end
irb(main):004:1> end
=> nil
irb(main):005:0> module B
irb(main):006:1> include A
irb(main):007:1> end
=> B
irb(main):008:0> module B
irb(main):009:1> module H
irb(main):010:2> end
irb(main):011:1> end
=> nil
irb(main):012:0> A::H.object_id == B::H.object_id
=> false
irb(main):014:0> B::H
=> B::H # it exists
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top