Panda, Tiger & Eagle levels complete#9
Panda, Tiger & Eagle levels complete#9ralphos wants to merge 7 commits intoRubyoffRails:masterfrom ralphos:master
Conversation
There was a problem hiding this comment.
We talked about this code in office hours... just an alternative way:
class Automobile
def ==(other)
color == other.color && make == other.make && model == other.model
end
end
####
blue_honda_accord = Automobille.new( make: "Honda", model: "Accord", color: "blue" )
@@vehicles.select { |vehicle| vehicle.class == Automobile }.select {|a| a == blue_honda_accord }
|
Got carried away playing with my code! Here are some of my takeaways: I first tried out using inheritance to reduce duplication of the add_vehicles method in the Automobile and Motorcycle class. I then decided that while it would make the code DRYer it wasn't obvious what the add_vehicles method was doing in those classes and you would have to check the superclass of Vehicle to see what it did. Which then begs the question what if Vehicle is inheriting the method from another superclass which defines the method. Seems like inheritance could cause a few headaches? So I then tried another way and got carried away with the whole object-oriented approach and created a VehicleCollector class. Probably overkill but the idea was to create a class with the sole intention of 'carrying out tasks' on the @@vehicles variable. Basically, it takes care of adding vehicles and showing them. Anyway, had fun trying different things out and experimenting! |
…d and included it in Vehicle class
|
Good use of modules of VehicleCollector to extend the vehicle with functionality. Looks nice |
There was a problem hiding this comment.
When you reach out to VehicleCollector -- you're actually not using the module you just included at all. Instead, you're reaching out to the main VehicleCollector module and using its methods.
it's a bit confusing, but you'll likely want to either not include the VehicleCollector at all, and just use VehicleCollector as a "singleton" module, or you'll want to:
def initialize
self.class.add_vehicles(vehicle)
end
There was a problem hiding this comment.
So correct me if I'm wrong, but the intention of creating a "singleton" module would be to only create one instance of VehicleCollector which would be globally available? That way you know you're referencing the one and only VehicleCollector? How would create a "singleton" module? Would it be on the module itself or on the Vehicle class?
I tried implementing the code you provided above but I also couldn't get it to work. Specifically, I'm not sure what self is referencing in this context?
What do you mean I'm reaching out to the main VehicleCollector module?
Sorry, quite a few questions as I'm not sure what's going on!
There was a problem hiding this comment.
Yes, a singleton class would mean there is just one global collection. Example of a singleton type collector:
module Collection
@@items = []
def self.add_to_collection(item)
@@items << item
end
def self.all_items
@@items
end
end
Collection.add_to_collection :the_item
Collection.all_items
=> [:the_item]
There was a problem hiding this comment.
"What do you mean I'm reaching out to the main VehicleCollector module?"
You're doing the equivalent of "Collection.add_to_collection :the_item" in the above example.
Didn't have much time to do these so I just did the bare minimum as per the requirements you set out (all using TDD though!)