Objects within objects...?

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Wed, 19 Jul 2006 07:26:36 +0200, Brandon Coleman <metrix1978@gmail.com> wrote:

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

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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/\.

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: module Kernel - RDoc Documentation

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 (name)
     @items[name]
   end
   def method_missing(methId)
      str = methId.id2name
      self.(str)
   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

···

On Wed, 19 Jul 2006 14:56:24 +0200, Brandon Coleman <metrix1978@gmail.com> wrote:

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

Brandon

···

--
Posted via http://www.ruby-forum.com/.

My pleasure.

Wes

···

On Thu, 20 Jul 2006 03:24:05 +0200, Brandon Coleman <metrix1978@gmail.com> wrote:

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

Brandon