Objects within objects... ??

B

Brandon Coleman

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
 
W

Wes Oldenbeuving

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


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
 
B

Brandon Coleman

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
 
D

Dave Rose

Brandon said:
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....
 
W

Wes Oldenbeuving

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

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
 
B

Brandon Coleman

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

Brandon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top