Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panda, Tiger & Eagle levels complete #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Panda, Tiger & Eagle levels complete #9

wants to merge 7 commits into from

Conversation

ralphos
Copy link

@ralphos ralphos commented Dec 21, 2012

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!)

automobile.make == 'Honda' &&
automobile.model == 'Accord'
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 }

@ralphos
Copy link
Author

ralphos commented Dec 23, 2012

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!

@jwo
Copy link
Member

jwo commented Dec 26, 2012

Good use of modules of VehicleCollector to extend the vehicle with functionality. Looks nice

include VehicleCollector

def initialize(vehicle)
VehicleCollector.add_vehicles(vehicle)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants