Created
February 11, 2015 05:02
-
-
Save mikepack/879f2d66c005df5febba to your computer and use it in GitHub Desktop.
Polymorphic Cars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car | |
def drive | |
# get_in | |
start_engine | |
accelerate | |
# park | |
end | |
end | |
class Ford < Car | |
def start_engine | |
puts 'Starting the Ford' | |
end | |
def accelerate | |
puts 'Driving my Ford!' | |
end | |
end | |
class Subaru < Car | |
def start_engine | |
puts 'Starting the Subaru' | |
end | |
def accelerate | |
puts 'Driving my Subaru!' | |
end | |
end | |
class BMW < Car | |
def start_engine | |
puts 'Starting the BMW' | |
end | |
def accelerate | |
puts 'Driving my BMW!' | |
end | |
end | |
class Broker | |
CARS = [Ford, Subaru, BMW] | |
def car | |
index = rand(0...CARS.count) | |
CARS[index].new | |
end | |
end | |
Broker.new.car.drive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment