[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Objects within objects... ??

brandon coleman

7/19/2006 5:27:00 AM

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

6 Answers

Wes Oldenbeuving

7/19/2006 6:50:00 AM

0

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
>


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

brandon coleman

7/19/2006 12:56:00 PM

0


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

dave rose

7/19/2006 1:21:00 PM

0

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

Wes Oldenbeuving

7/19/2006 1:28:00 PM

0

On Wed, 19 Jul 2006 14:56:24 +0200, Brandon Coleman <metrix1978@gmail.com>
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.ht...

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

brandon coleman

7/20/2006 1:24:00 AM

0

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

Brandon

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

Wes Oldenbeuving

7/20/2006 5:43:00 PM

0

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. :)
>
> Brandon
>

My pleasure.


Wes