Skip to content

Instantly share code, notes, and snippets.

@mikepack
Created February 11, 2015 05:02
Show Gist options
  • Save mikepack/879f2d66c005df5febba to your computer and use it in GitHub Desktop.
Save mikepack/879f2d66c005df5febba to your computer and use it in GitHub Desktop.
Polymorphic Cars
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