Project

General

Profile

Actions

Bug #4795

closed

Nested classes don't seem to resolve correctly when another class exists with the same name

Added by jxfruby (John Feminella) almost 13 years ago. Updated almost 13 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
Backport:
[ruby-core:36528]

Description

in /tmp/foo.rb

module Foo
end

module Foo::Bar
class Baz; end
end

class Baz
def say
"::Baz"
end
end

class Foo::Bar::Baz
def say
"::Foo::Bar::Baz"
end

def x
Baz.new.say
end
end

in irb:

load '/tmp/foo.rb'
=> true
Foo::Bar::Baz.new.x
=> "::Baz" # expected "::Foo::Bar::Baz"

=begin
This doesn't seem like the expected result. Have I misunderstood how constants work?
=end

Updated by andhapp (Anuj Dutta) almost 13 years ago

The call to 'Baz' in method 'x' will look up the ancestor chain and find the Baz constant defined for Object(because of class Baz) and uses it. However, if you qualify the call to Baz like this:

def x
Foo::Bar::Baz.new.say
end

Then it will give the desired result.

Updated by jxfruby (John Feminella) almost 13 years ago

But isn't Baz defined at the immediate Foo::Bar scope? Why does it keep going up the chain?

Updated by judofyr (Magnus Holm) almost 13 years ago

class Foo::Bar::Baz does not open the Foo::Bar scope. class Foo::Bar; class Baz does however.
// Magnus Holm

On Sat, May 28, 2011 at 16:51, John Feminella wrote:

Issue #4795 has been updated by John Feminella.

But isn't Baz defined at the immediate Foo::Bar scope? Why does it keep going up the chain?

Bug #4795: Nested classes don't seem to resolve correctly when another class exists with the same name
http://redmine.ruby-lang.org/issues/4795

Author: John Feminella
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

in /tmp/foo.rb

module Foo
end

module Foo::Bar
 class Baz; end
end

class Baz
 def say
   "::Baz"
 end
end

class Foo::Bar::Baz
 def say
   "::Foo::Bar::Baz"
 end

 def x
   Baz.new.say
 end
end

in irb:

load '/tmp/foo.rb'
 => true
Foo::Bar::Baz.new.x
 => "::Baz" # expected "::Foo::Bar::Baz"

=begin
This doesn't seem like the expected result. Have I misunderstood how constants work?
=end

--
http://redmine.ruby-lang.org

Updated by nobu (Nobuyoshi Nakada) almost 13 years ago

  • Status changed from Open to Rejected

As Magnus Holm has explained, it's the spec.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0