Skip to content

Instantly share code, notes, and snippets.

@abrahamsangha
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 19, 2015 07:28
Show Gist options
  • Save abrahamsangha/5918548 to your computer and use it in GitHub Desktop.
Save abrahamsangha/5918548 to your computer and use it in GitHub Desktop.
class Vehicle
def initialize(args)
@color = args[:color]
@wheels = args[:wheels]
@gas_mileage = [true,false]
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def needs_gas?
return @gas_mileage.sample
end
def stunt #3) get creative, add functionality to vehicle class, and subclass
@status = :cruising
end
end
class Car < Vehicle
def initialize(args)
super(args)
@wheels = 4
@gas_mileage = [true,true,false]
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
super
@num_seats = args[:num_seats]
@fare = args[:fare]
@passengers=[]
@gas_mileage = [true,true,true,false]
end
def drive
return self.brake if stop_requested?
super
end
def admit_passenger(passenger,money)
@passengers << passenger if money > @fare
end
def stop_requested?
return [true,false].sample
end
def stunt
return "You can't stunt in a bus"
end
end
class Motorbike < Vehicle
def initialize(args)
super
@wheels = 2
@gas_mileage = [true,false,false,false]
end
def drive
super
@speed = :fast
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
p volvo = Car.new({color: :red})
p volvo.drive
p volvo.brake
p volvo.needs_gas?
p '*******'
p megabus = Bus.new({color: :purple, wheels: 12, num_seats: 60, fare: 25})
p megabus.drive
p megabus.admit_passenger("Ghostface", 30)
p megabus.brake
p megabus.stop_requested?
p megabus.needs_gas?
p megabus.drive
p megabus.drive
p megabus.drive
p megabus.drive
p megabus.drive
p '*******'
p ninja = Motorbike.new({color: :black})
p ninja.drive
p ninja.brake
p ninja.needs_gas?
p ninja.weave_through_traffic
p megabus.stunt
p volvo.stunt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment