# Newbie q on class Foo::Bar

**URL:** <https://www.ruby-forum.com/t/newbie-q-on-class-foo-bar/73771>\
**Category:** Ruby\
**Created:** [October 26, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/newbie-q-on-class-foo-bar/73771 "2006-10-26T23:00:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 26, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/newbie-q-on-class-foo-bar/73771/1 "2006-10-26T23:00:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 26, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/newbie-q-on-class-foo-bar/73771/2 "2006-10-26T23:10:53Z")

</div>

matt neuburg wrote:

> 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.

There are other reasons, this is just one.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 26, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/newbie-q-on-class-foo-bar/73771/3 "2006-10-26T23:16:54Z")

</div>

Hi –

On Fri, 27 Oct 2006, Paul L. wrote:

> > And then I can say  
> > 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 27, 2006, 1:21am UTC](https://www.ruby-forum.com/t/newbie-q-on-class-foo-bar/73771/4 "2006-10-27T01:21:12Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > class Bar  
> > > doing so? I take it that Bar is not magically related to Foo - it can’t  
> > > 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.
