Skip to content

Instantly share code, notes, and snippets.

@tambling
Forked from aespaldi/vehicle.rb
Last active December 18, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tambling/5760659 to your computer and use it in GitHub Desktop.
Save tambling/5760659 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :color, :wheels, :status
def initialize(args)
@wheels=4
@fuel_economy=[true, false]
@color = args[:color]
end
def drive
@status=:driving
end
def brake
@status=:stopped
end
def needs_gas?
return @fuel_economy.sample
end
end
class Car<Vehicle
def initialize(args)
super
@fuel_economy=[true, false, false]
end
end
class Bus<Vehicle
attr_reader :passengers
def initialize(args)
super
@fuel_economy=[true, true, true, false]
@wheels=args[:wheels]
@num_seats=args[:num_seats]
@fare=args[:fare]
@passengers=[]
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
end
class Motorbike<Vehicle
attr_reader :speed
def initialize(args)
super
@wheels=2
@fuel_economy=[true,false,false,false]
end
def drive
super
@speed=:fast
end
def weave_through_traffic
@status= :driving_like_a_crazy_person
end
end
car=Car.new( color: "blue")
puts car.color=="blue"
puts car.wheels==4
car.drive
puts car.status==:driving
car.brake
puts car.status==:stopped
bus=Bus.new(color: "red", wheels: 6, num_seats: 30, fare: 2)
puts bus.color=="red"
bus.admit_passenger("Jim Bob", 3)
puts bus.passengers==["Jim Bob"]
bus.drive
puts bus.status==:driving || bus.status==:stopped
motorcycle=Motorbike.new(color: "black")
puts motorcycle.color=="black"
motorcycle.weave_through_traffic
puts motorcycle.status==:driving_like_a_crazy_person
motorcycle.brake
puts motorcycle.status==:stopped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment