Module Scopes and Names

Discussion in 'Ruby' started by Brian Schroeder, Aug 24, 2004.

  1. Hello Group,

    I think that ruby is a bit too intelligent regarding module names.
    Consider the follwing situation

    module B
    class B; end
    end

    module A
    module B
    module C
    class C < B::B; end
    end
    end
    end

    This does not work, as ruby tries to load A::B::B instead of B::B as
    I supposed. So for example I can't create a class structure

    module MyProg
    module UI
    module Gnome
    class Something < Gnome::Canvas
    end
    end
    end

    even though this would seem a natural naming scheme to me.

    What is the reason behind this behaviour, and is it possible to avoid
    this problem without renaming my module to MyProg::UI::GnomeUI which
    seems redundant to me?

    regards,

    Brian Schröder

    --
    Brian Schröder
    http://www.brian-schroeder.de/
    http://ruby.brian-schroeder.de/
     
    Brian Schroeder, Aug 24, 2004
    #1
    1. Advertising

  2. Brian Schroeder

    T. Onoma Guest

    On Tuesday 24 August 2004 07:01 am, Brian Schroeder wrote:
    module B
    class B; end
    end

    module A
    module B
    module C
    class C < ::B::B; end
    end
    end
    end


    (I think, but never have tried)

    --
    T.
     
    T. Onoma, Aug 24, 2004
    #2
    1. Advertising

  3. "Brian Schroeder" <> schrieb im Newsbeitrag
    news:p...
    > Hello Group,
    >
    > I think that ruby is a bit too intelligent regarding module names.


    No, that's usual scope resolution practice as found in other languages (C++
    etc.). Module resolution starts always at the nearest possible position,
    which makes perfectly sense since it saves a lot of typing for the usual
    case, i.e. when you want to refer to something in a sibling module.

    > Consider the follwing situation
    >
    > module B
    > class B; end
    > end
    >
    > module A
    > module B
    > module C
    > class C < B::B; end


    As Mr. T already pointed out use the form ::B::B to explicitely start lookup
    from the top level.

    > end
    > end
    > end
    >
    > This does not work, as ruby tries to load A::B::B instead of B::B as
    > I supposed. So for example I can't create a class structure
    >
    > module MyProg
    > module UI
    > module Gnome
    > class Something < Gnome::Canvas
    > end
    > end
    > end
    >
    > even though this would seem a natural naming scheme to me.
    >
    > What is the reason behind this behaviour, and is it possible to avoid
    > this problem without renaming my module to MyProg::UI::GnomeUI which
    > seems redundant to me?


    See above.

    Regards

    robert
     
    Robert Klemme, Aug 24, 2004
    #3
  4. > As Mr. T already pointed out use the form ::B::B to explicitely start
    > lookup from the top level.


    Thank you all for the replies. This was a very helpful information. I just never
    read that ::X::Y starts at the top level - so as always it was not ruby too
    intelligent, but I missed the important point.

    regards,

    Brian


    --
    Brian Schröder
    http://www.brian-schroeder.de/
     
    Brian Schroeder, Aug 24, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Benjamin

    Nested scopes and class variables

    Dave Benjamin, Jan 31, 2005, in forum: Python
    Replies:
    7
    Views:
    401
    Steven Bethard
    Feb 3, 2005
  2. Tim N. van der Leeuw

    Nested scopes, and augmented assignment

    Tim N. van der Leeuw, Jul 4, 2006, in forum: Python
    Replies:
    39
    Views:
    800
    Piet van Oostrum
    Jul 10, 2006
  3. Pyenos
    Replies:
    9
    Views:
    312
    Fredrik Lundh
    Dec 23, 2006
  4. Hal Fulton
    Replies:
    7
    Views:
    167
  5. Replies:
    12
    Views:
    471
    Øyvind Røtvold
    Oct 7, 2012
Loading...

Share This Page