Beware of Ruby namespaces
Posted by Paolo Fri, 17 Aug 2007 20:02:00 GMT
X=100
module A
module B
X=200
end
end
# ------- Case 1 ------------
module A
module B
class C
p X
p self.class.nesting
end
end
end
# this produces:
# 200
#[A::B::C, A::B, A]
# ------- Case 2 ------------
class A::B::C
p X
p self.class.nesting
end
# this produces:
# 100
#[A::B::C]This post is basically a note to myself: the :: notation in Ruby is NOT a shortcut for nesting modules. In the second case class C is not able to see the X constant inside B (and shows the value of the outer one).







