newbie q on class Foo::Bar

Discussion in 'Ruby' started by matt neuburg, Oct 26, 2006.

  1. matt neuburg

    matt neuburg Guest

    What's the point of a class-within-a-class? For example, I take it that
    I can say:

    class Foo
    class Bar
    def whatever
    ...

    And then I can say

    my_foo_bar = Foo::Bar.new

    For example, isn't that what something like File::Stat is?

    My question is, am I right about this, and if so, what's the point of
    doing so? I take it that Bar is not magically related to Foo - it can't
    see Foo's methods or anything like that, can it? So is this merely a way
    of using a class (Foo) as a namespace so that related classes (like Bar)
    are all in one place? Thx - m.

    --
    matt neuburg, phd = , http://www.tidbits.com/matt/
    Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt neuburg, Oct 26, 2006
    #1
    1. Advertising

  2. matt neuburg

    Guest

    Hi --

    On Fri, 27 Oct 2006, Paul Lutus wrote:

    > matt neuburg wrote:
    >
    >> What's the point of a class-within-a-class? For example, I take it that
    >> I can say:
    >>
    >> class Foo
    >> class Bar
    >> def whatever
    >> ...
    >>
    >> And then I can say
    >>
    >> my_foo_bar = Foo::Bar.new
    >>
    >> For example, isn't that what something like File::Stat is?
    >>
    >> My question is, am I right about this, and if so, what's the point of
    >> doing so? I take it that Bar is not magically related to Foo - it can't
    >> see Foo's methods or anything like that, can it? So is this merely a way
    >> of using a class (Foo) as a namespace so that related classes (like Bar)
    >> are all in one place? Thx - m.

    > One reason is to create a class that is private. You may be aware that one
    > wants to separate results from implementation (separate the result from the
    > method used to produce the result), this can extend to classes that need to
    > exist, but don't need to be made available as part of the interface. That
    > way, the programmer can change the class without having to ask what
    > external effects this might have.


    The nesting won't give you privacy, though; the class can still be
    accessed from outside. You could of course not document it, but
    that's no more true of a nested class than a non-nested one.


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
    , Oct 26, 2006
    #2
    1. Advertising

  3. matt neuburg

    matt neuburg Guest

    <> wrote:

    > Hi --
    >
    > On Fri, 27 Oct 2006, Paul Lutus wrote:
    >
    > > matt neuburg wrote:
    > >
    > >> What's the point of a class-within-a-class? For example, I take it that
    > >> I can say:
    > >>
    > >> class Foo
    > >> class Bar
    > >> def whatever
    > >> ...
    > >>
    > >> And then I can say
    > >>
    > >> my_foo_bar = Foo::Bar.new
    > >>
    > >> For example, isn't that what something like File::Stat is?
    > >>
    > >> My question is, am I right about this, and if so, what's the point of
    > >> doing so? I take it that Bar is not magically related to Foo - it can't
    > >> see Foo's methods or anything like that, can it? So is this merely a way
    > >> of using a class (Foo) as a namespace so that related classes (like Bar)
    > >> are all in one place? Thx - m.

    > > One reason is to create a class that is private. You may be aware that one
    > > wants to separate results from implementation (separate the result from the
    > > method used to produce the result), this can extend to classes that need to
    > > exist, but don't need to be made available as part of the interface. That
    > > way, the programmer can change the class without having to ask what
    > > external effects this might have.

    >
    > The nesting won't give you privacy, though; the class can still be
    > accessed from outside. You could of course not document it, but
    > that's no more true of a nested class than a non-nested one.


    I'm glad you're weighing in here, since, though this is a common enough
    Ruby idiom, Rails is the case in point that got me thinking about this.
    So, for example, have ActionController::Base and
    ActionController::AbstractRequest something in common (other than being
    packaged in the same namespace)? Thx - m.

    --
    matt neuburg, phd = , http://www.tidbits.com/matt/
    Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt neuburg, Oct 27, 2006
    #3
    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. Replies:
    3
    Views:
    374
    John Roth
    Jul 29, 2005
  2. Jonathan Bartlett
    Replies:
    7
    Views:
    442
    Peter Nilsson
    Jul 8, 2005
  3. Wejn
    Replies:
    2
    Views:
    109
    Michal
    Nov 29, 2003
  4. Max Williams
    Replies:
    10
    Views:
    193
    Max Williams
    Dec 15, 2007
  5. Gunnar Hjalmarsson
    Replies:
    12
    Views:
    293
    Darren Dunham
    Feb 24, 2005
Loading...

Share This Page