Skip to content

Instantly share code, notes, and snippets.

@stomatocode
Forked from aespaldi/vehicle.rb
Last active December 18, 2015 09:19
Show Gist options
  • Save stomatocode/5760727 to your computer and use it in GitHub Desktop.
Save stomatocode/5760727 to your computer and use it in GitHub Desktop.
class Vehicle
def powerplant(args)
@engine
@transmission
@drivetrain
end
def chassis(args)
@suspension
@orientation
end
def drive
@status = :driving
end
def speed
if @status == :driving
@speed = :fast
else
@status = :slow
end
end
def brake
@status = :stopped
end
def needs_gas?
return [true,true,false].sample
end
end
class Car < Vehicle
@@WHEELS = 4
def initialize(args)
@color = args[:color]
@wheels = @@WHEELS
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
@color = args[:color]
@wheels = args[:wheels]
@num_seats = args[:num_seats]
@fare = args[:fare]
@passengers=[]
end
def drive
return self.brake if stop_requested?
@status = :driving
end
def admit_passenger(passenger,money)
money > @fare ? @passengers << passenger : "get off the bus!"
end
def stop_requested?
return [true,false].sample
end
end
class Motorbike < Vehicle
@@WHEELS = 2
def initialize(args)
@color = args[:color]
@wheels = @@WHEELS
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
########### "Driver" Code Below ############################################
bus_args = {:color => "yellow", :wheels => 6, :num_seats => 25, :fare => 2}
other_bus_args = {color: "silver", wheels: 8, num_seats: 30, fare: 2}
tesla_args = {color: "blue"}
school = Bus.new(bus_args)
p school
p school.admit_passenger("Jake", 1)
ducati = Motorbike.new(:color => "red")
p ducati.weave_through_traffic
p school.stop_requested?
tesla = Car.new(tesla_args)
p tesla
p tesla.drive
p tesla.speed
p tesla.needs_gas?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment