# Objects within objects...?

**URL:** <https://www.ruby-forum.com/t/objects-within-objects/98946>\
**Category:** Ruby\
**Created:** [July 19, 2006, 7:26am UTC](https://www.ruby-forum.com/t/objects-within-objects/98946 "2006-07-19T07:26:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 19, 2006, 7:26am UTC](https://www.ruby-forum.com/t/objects-within-objects/98946/1 "2006-07-19T07:26:24Z")

</div>

I have a thought in my head, but I don’t know how to implement it, or  
even if I am thinking about it in the right way… what I want to do is  
store information in a header detail type way, but I would like to be  
able to use methods directly on my data. I guess the example I have  
pictured in my head is:  
dealership\_obj = dealership.new  
and within that If I had data on vehicles it would be:  
dealership.honda.new(data)  
dealership.ford.new(data)  
etc.

Am I thinking about this right? is there a better way but I just don’t  
see it?

any ideas or links to web pages would be greatly appreciated!

Brandon

---

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 19, 2006, 8:56am UTC](https://www.ruby-forum.com/t/objects-within-objects/98946/2 "2006-07-19T08:56:16Z")

</div>

On Wed, 19 Jul 2006 07:26:36 +0200, Brandon Coleman  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Am I thinking about this right? is there a better way but I just don’t  
> see it?
> 
> any ideas or links to web pages would be greatly appreciated!
> 
> Brandon

For your example, is the following what you were thinking about?

------- Code --------

class Dealership  
attr\_accessor :honda, :ford  
def initialize  
@honda=Honda.new  
@ford=Ford.new  
end  
end  
class Honda  
def to\_s  
return “Honda”  
end  
end  
class Ford  
def to\_s  
return “Ford’s to-string”  
end  
end

dealership\_obj = Dealership.new  
puts dealership\_obj.honda  
puts dealership\_obj.ford

------- End Code --------

Wes Oldenbeuving

---

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 19, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/objects-within-objects/98946/3 "2006-07-19T14:56:05Z")

</div>

> dealership\_obj = Dealership.new  
> puts dealership\_obj.honda  
> puts dealership\_obj.ford

Yes it is! but I would like to be able to add and delete objects on the  
fly. For instance I want to add Toyota: could the objects be stored in  
an array so that I could add an object and delete an object when I  
needed? I would like to be able to say something like  
dealership.add(toyota.new(etc etc)) dealership.puts(toyota) I think  
talking about it is showing the weakness of my idea. because then I  
would probably need a dealership.modify(toyota.something()) and I would  
really like to nest my objects about 4 deep.  
Talking about it brought me to this example:

dealership.modify(toyota.modify(shelfb.add(bin1.new(carburetor)))).

which IS what I think I would like to do, I just think that code is ugly  
and illedgible, and so I feel there is probably a better way.

Brandon

---

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 19, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/objects-within-objects/98946/4 "2006-07-19T15:20:37Z")

</div>

Brandon Coleman wrote:

> > dealership\_obj = Dealership.new  
> > puts dealership\_obj.honda  
> > puts dealership\_obj.ford
> 
> Yes it is! but I would like to be able to add and delete objects on the  
> fly. For instance I want to add Toyota: could the objects be stored in  
> an array so that I could add an object and delete an object when I  
> needed? I would like to be able to say something like  
> dealership.add(toyota.new(etc etc)) dealership.puts(toyota) I think  
> talking about it is showing the weakness of my idea. because then I  
> would probably need a dealership.modify(toyota.something()) and I would  
> really like to nest my objects about 4 deep.  
> Talking about it brought me to this example:
> 
> dealership.modify(toyota.modify(shelfb.add(bin1.new(carburetor)))).
> 
> which IS what I think I would like to do, I just think that code is ugly  
> and illedgible, and so I feel there is probably a better way.
> 
> Brandon  
> …brandon…while i’m still a newbie at this…a perfect example is  
> the free tutorial learn-ruby…that does exactly what you want but with  
> address book as an example…each person is an address entry (really a  
> hash storing name,email,address,city,state) aka a car object (with hash  
> keys like carb tranny,etc…)…then an address book  
> is an object collection of many persons objects collected into an  
> array…

---

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 20, 2006, 3:23am UTC](https://www.ruby-forum.com/t/objects-within-objects/98946/5 "2006-07-20T03:23:52Z")

</div>

Thank you for taking the time to answer my question! that REALLY helped  
push me in the right direction. 🙂

Brandon

---

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 20, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/objects-within-objects/98946/6 "2006-07-20T19:46:37Z")

</div>

On Thu, 20 Jul 2006 03:24:05 +0200, Brandon Coleman  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thank you for taking the time to answer my question! that REALLY helped  
> push me in the right direction. 🙂
> 
> Brandon

My pleasure.

Wes

---

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 19, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/objects-within-objects/98946/7 "2006-07-19T15:37:34Z")

</div>

On Wed, 19 Jul 2006 14:56:24 +0200, Brandon Coleman  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Yes it is! but I would like to be able to add and delete objects on the  
> fly. For instance I want to add Toyota: could the objects be stored in  
> an array so that I could add an object and delete an object when I  
> needed?

I think that the easiest way to achieve something like this is to use a  
hash.

Another way would be to try and setup something with  
Kernel#method\_missing. That way you could redirect invalid method calls  
to  
the hash, or whichever way you use to track things internally. The docs  
for Kernel#method\_missing:  
[http://www.ruby-doc.org/core/classes/Kernel.html#M002946](http://www.ruby-doc.org/core/classes/Kernel.html#M002946)

Here is an example using both methods:

----- Code -----

class Dealership  
def initialize  
@items=Hash.new  
end  
def add(name,new\_item)  
@items[name]=new\_item  
end  
def   
@items[name]  
end  
def method\_missing(methId)  
str = methId.id2name  
self.  
end  
end

class Honda  
def to\_s  
return “Honda”  
end  
end

class Ford  
def sound\_horn  
puts “You sound the Ford’s horn!”  
end  
end

dealership\_obj = Dealership.new

dealership\_obj.add(“honda”, Honda.new)  
dealership\_obj.add(“ford”, Ford.new)

puts dealership\_obj[“honda”]  
dealership\_obj[“ford”].sound\_horn  
dealership\_obj.ford.sound\_horn

----- End code -----

> Talking about it brought me to this example:
> 
> dealership.modify(toyota.modify(shelfb.add(bin1.new(carburetor)))).
> 
> which IS what I think I would like to do, I just think that code is ugly  
> and illedgible, and so I feel there is probably a better way.

By putting the hash and method\_missing inside the car brand classes and  
classes that will be contained by them, your given example would look  
like:

dealership.toyota.shelfb.add(‘bin1’,Bin1.new(carburetor))

Cheers,

Wes
