# Nested classes

**URL:** <https://www.ruby-forum.com/t/nested-classes/102499>\
**Category:** Ruby\
**Created:** [November 16, 2005, 10:08am UTC](https://www.ruby-forum.com/t/nested-classes/102499 "2005-11-16T10:08:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![groleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/51bf81/32.png) [@groleo](https://www.ruby-forum.com/u/groleo)\
**Post date:** [November 16, 2005, 10:08am UTC](https://www.ruby-forum.com/t/nested-classes/102499/1 "2005-11-16T10:08:01Z")

</div>

hi list.

I have the following code:

#! /usr/bin/ruby

class B  
def fun\_b  
p “B”  
end  
end

class A  
b = B.new  
end

a = A.new  
a.b.fun\_b

The part that bugs me is this error:  
./test.rb:15: undefined method `b’ for #\<A:0xb7cafb24\> (NoMethodError)

What is the way to code the above ideea, so that the last line would be  
correct.

I’m running ruby 1.8.3 (2005-09-21) [i486-linux].

–  
Regards, Groleo!

# Use “Reply to All” on mailing lists.

# touch universe

# chmod +x universe

# ./universe

---

<div class="post-metadata">

**Author:** ![groleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/51bf81/32.png) [@groleo](https://www.ruby-forum.com/u/groleo)\
**Post date:** [November 16, 2005, 10:14am UTC](https://www.ruby-forum.com/t/nested-classes/102499/2 "2005-11-16T10:14:06Z")

</div>

On 16/11/05, Groleo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> ./test.rb:15: undefined method `b’ for #\<A:0xb7cafb24\> (NoMethodError)
> 
> What is the way to code the above ideea, so that the last line would be correct.
> 
> I’m running ruby 1.8.3 (2005-09-21) [i486-linux].

What problem do you want to solve?

Is this maybe nearer to you goals:

class A  
class B  
def fun\_b  
p “B”  
end  
end  
end

A::B.new.fun\_b

cheers,

Brian

–  
[http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)

Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![groleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/51bf81/32.png) [@groleo](https://www.ruby-forum.com/u/groleo)\
**Post date:** [November 16, 2005, 10:26am UTC](https://www.ruby-forum.com/t/nested-classes/102499/3 "2005-11-16T10:26:19Z")

</div>

Groleo M. schrieb:

> a = A.new  
> a.b.fun\_b
> 
> The part that bugs me is this error:  
> ./test.rb:15: undefined method `b’ for #\<A:0xb7cafb24\> (NoMethodError)
> 
> What is the way to code the above ideea, so that the last line would be correct.

Maybe this is what you want:

class A  
attr\_reader :b  
def initialize  
@b = B.new  
end  
end

Note that here each instance of A has its own instance of B, which might  
not be what you want.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![groleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/51bf81/32.png) [@groleo](https://www.ruby-forum.com/u/groleo)\
**Post date:** [November 16, 2005, 10:44am UTC](https://www.ruby-forum.com/t/nested-classes/102499/4 "2005-11-16T10:44:26Z")

</div>

On 16/11/05, Groleo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > end
> > 
> > ```
> 
> Consider the case when class B would have more member functions.  
> end

Maybe you should look at the delegation pattern. That may be  
applicable here. But I have not yet understood your use case. Could  
you describe it in a bit more detail?

regards,

Brian

–  
[http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)

Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![groleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/51bf81/32.png) [@groleo](https://www.ruby-forum.com/u/groleo)\
**Post date:** [November 16, 2005, 11:02am UTC](https://www.ruby-forum.com/t/nested-classes/102499/5 "2005-11-16T11:02:37Z")

</div>

On 11/16/05, Brian Schröder [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > p "B"
> > > 
> > > ```
> > 
> > ```
> > p "_B"
> > 
> > ```
> > 
> > I couldn’t find any hint on google though ☹
> 
> Maybe you should look at the delegation pattern. That may be  
> applicable here. But I have not yet understood your use case. Could  
> you describe it in a bit more detail?

Sure.  
Consider a client script that has to querry a server.  
The server operate on users/domains of a running qmail.

So, I would have a class named Client, with the folowing members:  
login( user, pass ) logins to the remote administration server  
domain an instantion of a domain class.

OTOH Domain class has X member functions:  
add( domain\_name) adds a new domain  
remove  
update

So a scenario would look like:

c =Client.new #creates a new Domain object  
c.login( “foo”, “bar” )  
c.domain.add( “localdomain” )

Now , back to the code ,the attr\_reader was the magic word from what  
Pit posted .  
Maybe you too observed, but why in ruby  
this attr\_reader : b is different from attr\_reader :b  
^space ^no space

–  
Regards, Groleo!

# Use “Reply to All” on mailing lists.

# touch universe

# chmod +x universe

# ./universe

---

<div class="post-metadata">

**Author:** ![groleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/51bf81/32.png) [@groleo](https://www.ruby-forum.com/u/groleo)\
**Post date:** [October 9, 2013, 8:01pm UTC](https://www.ruby-forum.com/t/nested-classes/102499/6 "2013-10-09T20:01:03Z")

</div>

On Oct 8, 2013, at 9:40 AM, Xeno C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Is it good idea to use nested classes if they are small and will be used by  
> > parent class only?
> > 
> > Bob

You can check out any time you like, but you can never leave.

---

<div class="post-metadata">

**Author:** ![groleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/51bf81/32.png) [@groleo](https://www.ruby-forum.com/u/groleo)\
**Post date:** [November 16, 2005, 11:20am UTC](https://www.ruby-forum.com/t/nested-classes/102499/7 "2005-11-16T11:20:42Z")

</div>

On 16/11/05, Groleo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > ```
> > > > def fun_b
> > > > 
> > > > ```
> > > 
> > > ```
> > > def other_b
> > > 
> > > ```
> > > 
> > > network protocol.
> 
> c =Client.new #creates a new Domain object  
> c.login( “foo”, “bar” )  
> c.domain.add( “localdomain” )
> 
> Now , back to the code ,the attr\_reader was the magic word from what  
> Pit posted .  
> Maybe you too observed, but why in ruby  
> this attr\_reader : b is different from attr\_reader :b  
> ^space ^no space

Because :b is the symbol :b while : b is a colon followed by a b which  
is not allowed syntax here.

cheers,

Brian

–  
[http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)

Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![groleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/51bf81/32.png) [@groleo](https://www.ruby-forum.com/u/groleo)\
**Post date:** [November 16, 2005, 11:35am UTC](https://www.ruby-forum.com/t/nested-classes/102499/8 "2005-11-16T11:35:48Z")

</div>

Groleo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> […]
> 
> Now , back to the code ,the attr\_reader was the magic word from what  
> Pit posted .  
> Maybe you too observed, but why in ruby  
> this attr\_reader : b is different from attr\_reader :b  
> ^space ^no space

Because colon-identifier represents a symbol, and attr\_reader takes  
symbols as arguments. The colon is not a separator. For example:]

attr\_reader :a, :b, :c

Each of :a, :b, and :c are symbols.

---

<div class="post-metadata">

**Author:** ![groleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/51bf81/32.png) [@groleo](https://www.ruby-forum.com/u/groleo)\
**Post date:** [November 16, 2005, 10:35am UTC](https://www.ruby-forum.com/t/nested-classes/102499/9 "2005-11-16T10:35:22Z")

</div>

On 11/16/05, Brian Schröder [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> end
> 
> A::B.new.fun\_b
> 
> cheers,
> 
> Brian

Consider the case when class B would have more member functions.  
In your example I would have to create a new object for each call to a  
B function.  
OTOH, a solution that I dont like 🙂 could be :  
class A  
class B  
def fun\_b  
p “B”  
end  
def other\_b  
p “\_B”  
end  
end  
end  
b = A::B.new  
b.fun\_b  
b.other\_b

By the instantion of a new B object in the code from the first post,  
and refering to it, I was searching to keep a hierarchy inside a  
network protocol.  
I couldn’t find any hint on google though ☹

–  
Regards, Groleo!

# Use “Reply to All” on mailing lists.

# touch universe

# chmod +x universe

# ./universe
