Skip to content

Instantly share code, notes, and snippets.

@carterS
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 24, 2015 06:29
Show Gist options
  • Save carterS/6756910 to your computer and use it in GitHub Desktop.
Save carterS/6756910 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :color, :wheels, :status
def initialize(attributes = {})
@wheels = attributes[:wheels]
@color = attributes[:color]
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
end
class Car < Vehicle
def needs_gas?
return [true,true,false].sample
end
end
class Bus < Vehicle
attr_reader :num_seats, :fare, :passengers
def initialize(attributes = {})
@num_seats = attributes[:num_seats]
@fare = attributes[:fare]
@passengers=[]
super(attributes)
end
def drive
return self.brake if stop_requested?
@status = :driving
end
def admit_passenger(passenger,money)
@passengers << passenger if money > @fare
end
def stop_requested?
return [true,false].sample
end
def needs_gas?
return [true,true,true,false].sample
end
end
class Motorbike < Vehicle
attr_reader :speed
def drive
@speed = :fast
super
end
def brake
@speed = :zero
super
end
def needs_gas?
return [true,false,false,false].sample
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
# Quick Tests
# car1 = Car.new({:color => 'midnight-blue', :wheels => 4})
# p car1
# bus1 = Bus.new({:color => 'hot pink', :wheels => 8, :num_seats => 36, :fare => 1.25})
# p bus1
# excite_bike = Motorbike.new({:color => 'moby dick grey', :wheels => 2})
# bus1.admit_passenger("Evette", 1.50)
# p bus1.passengers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment