J
John Feminella
It was my understanding that consts are first searched for in their
own module scope before Ruby starts looking elsewhere. So, if you have
a class called X in a module M1::M2 and another one at top level, and
you're inside M1::M2, then referencing X gets you M1::M2::X.
This doesn't appear to be the case though:
ruby-1.9.2-p0 :001 > module Foo; end
=> nil
ruby-1.9.2-p0 :002 > module Foo::Bar; class Baz; end; end
=> nil
ruby-1.9.2-p0 :003 > class Baz; def say; "::Baz"; end; end
=> nil
ruby-1.9.2-p0 :004 > class Foo::Bar::Baz; def say; "::Foo::Bar::Baz";
end; def x; Baz.new.say; end; end
=> nil
# expected "Foo::Bar::Baz"
ruby-1.9.2-p0 :005 > Foo::Bar::Baz.new.x
=> "::Baz"
Have I misunderstood how constants work?
~ jf
own module scope before Ruby starts looking elsewhere. So, if you have
a class called X in a module M1::M2 and another one at top level, and
you're inside M1::M2, then referencing X gets you M1::M2::X.
This doesn't appear to be the case though:
ruby-1.9.2-p0 :001 > module Foo; end
=> nil
ruby-1.9.2-p0 :002 > module Foo::Bar; class Baz; end; end
=> nil
ruby-1.9.2-p0 :003 > class Baz; def say; "::Baz"; end; end
=> nil
ruby-1.9.2-p0 :004 > class Foo::Bar::Baz; def say; "::Foo::Bar::Baz";
end; def x; Baz.new.say; end; end
=> nil
# expected "Foo::Bar::Baz"
ruby-1.9.2-p0 :005 > Foo::Bar::Baz.new.x
=> "::Baz"
Have I misunderstood how constants work?
~ jf