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: