Skip to content

Instantly share code, notes, and snippets.

@rttong
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 18, 2015 09:29
Show Gist options
  • Save rttong/5761220 to your computer and use it in GitHub Desktop.
Save rttong/5761220 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :color, :wheels, :status
def initialize(args)
@color = args[:color]
@status = :off
end
def brake
@status = :stopped
end
def drive
@status = :driving
end
def park
@status = :off
end
def needs_gas?
return [true,true,false].sample
end
end
class Car < Vehicle
@@WHEELS = 4
def initialize(args)
super
@wheels = @@WHEELS
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
super
@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
@@WHEELS = 2
def initialize(args)
super
@wheels = @@WHEELS
end
def drive
super
@speed = :fast
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
### BUS
puts "BUS"
my_bus = Bus.new({:color => "yellow", :wheels => 4, :num_seats => 30, :fare => 2})
my_bus.admit_passenger("Jake", 1)
my_bus.admit_passenger("John", 3)
p my_bus.drive == :driving
p my_bus.brake == :stopped
p my_bus.park == :off
p "Passengers on bus: #{my_bus.passengers}"
if (my_bus.needs_gas? == false || my_bus.needs_gas? == true)
puts "OK"
end
if (my_bus.stop_requested? == false || my_bus.stop_requested? == true)
puts "OK"
end
### CAR
puts
puts
puts "CAR"
my_car = Car.new({:color => "red"})
p my_car.color
p my_car.wheels
p my_car.drive == :driving
p my_car.brake == :stopped
if (my_car.needs_gas? == false || my_car.needs_gas? == true)
puts "OK"
end
### MOTORBIKE
puts
puts
puts "MOTORBIKE"
my_motorbike = Motorbike.new({:color => "black"})
p my_motorbike.color == "black"
p my_motorbike.drive == :fast
p my_motorbike.brake == :stopped
p my_motorbike.weave_through_traffic == :driving_like_a_crazy_person
if (my_motorbike.needs_gas? == true || my_motorbike.needs_gas? == false)
puts "OK"
end
class Car
@@WHEELS = 4
def initialize(args)
@color = args[:color]
@wheels = @@WHEELS
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def needs_gas?
return [true,true,false].sample
end
end
class Bus
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)
@passengers << passenger if money > @fare
end
def brake
@status = :stopped
end
def stop_requested?
return [true,false].sample
end
def needs_gas?
return [true,true,true,false].sample
end
end
class Motorbike
@@WHEELS = 2
def initialize(args)
@color = args[:color]
@wheels = @@WHEELS
end
def drive
@status = :driving
@speed = :fast
end
def brake
@status = :stopped
end
def needs_gas?
return [true,false,false,false].sample
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment