Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Unexpected behavior when referencing nested classes

Reply
Thread Tools

Unexpected behavior when referencing nested classes

 
 
John Feminella
Guest
Posts: n/a
 
      05-28-2011
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
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/

 
Reply With Quote
 
 
 
 
Josh Cheek
Guest
Posts: n/a
 
      05-28-2011
[Note: parts of this message were removed to make it a legal post.]

On Sat, May 28, 2011 at 7:21 AM, John Feminella <(E-Mail Removed)>wrote:

> 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
> --
> John Feminella
> Principal Consultant, BitsBuilder
> LI: http://www.linkedin.com/in/johnxf
> SO: http://stackoverflow.com/users/75170/
>
>


I have some vague hypotheses about why it does this, but don't have any idea
how to test them.



module Foo
end

module Foo::Bar
class Baz
end
end

class Baz
def say
"::Baz"
end
end


# gives the unexpected result
class Foo::Bar::Baz
def say
"::Foo::Bar::Baz"
end
def x # !> previous definition of x was here
Baz.new.say
end
end

Foo::Bar::Baz.new.x # => "::Baz"


# gives the expected result
module Foo::Bar
class Baz
def x # !> method redefined; discarding old x
Baz.new.say
end
end
end

Foo::Bar::Baz.new.x # => "::Foo::Bar::Baz"

 
Reply With Quote
 
 
 
 
Anurag Priyam
Guest
Posts: n/a
 
      05-28-2011
> Have I misunderstood how constants work?

Yeah. Ruby first tries to find the constant in the "lexical scope" of
the reference - immediately enclosing module/class, then the next
enclosing module/class, and so on.

You can find out the modules/classes that are searched by calling
Module.nesting method at that point.

> class Foo::Bar::Baz; def say; "::Foo::Bar::Baz";
> end; def x; Baz.new.say; end; end
> Foo::Bar::Baz.new.x


Here, Baz is first looked up in Foo::Bar::Baz, and then the global
namespace; confirm with Module.nesting. Ruby finds Baz in the later,
so '::Baz is the expected output.

module Foo::Bar
class Baz
def say; "::Foo::Bar::Baz"; end
def x; Baz.new.say; end
end
end

Foo::Bar::Baz.new.x #=> "::Foo::Bar::Baz"

Here, Ruby first looks up Baz in Foo::Bar::Baz, followed by Foo::Bar
finds, and then the global namespace. Again, you can confirm this with
Module.nesting.

Sometime back, I had answered a similar question. You might want to
have a look[1].

[1]: http://groups.google.com/group/ruby-...066bf9366e44f#

--
Anurag Priyam
http://about.me/yeban/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie: nested classes; ruby compiler behavior Doug S. Ruby 4 12-04-2010 07:54 PM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested classes within other classes Tony Johansson C++ 2 12-14-2004 11:41 AM
Nested Classes vs Top-Level Classes kelvSYC Java 2 08-18-2004 07:09 AM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM



Advertisments