Skip to content

Instantly share code, notes, and snippets.

@gsperka
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active August 29, 2015 13:57
Show Gist options
  • Save gsperka/9575687 to your computer and use it in GitHub Desktop.
Save gsperka/9575687 to your computer and use it in GitHub Desktop.
class MotorVehicle
def initialize(args={})
@color = args[:color]
@wheels = args[:wheels]
end
def brake
@status = :stopped
end
def drive
return self.brake if stop_requested
@speed
@status = :driving
end
def needs_gas?
@gas
end
end
class Car < MotorVehicle
attr_reader :stop_requested
def initialize(args={})
@color = args[:color]
@wheels = 4
@stop_requested = false
@gas = [true,true,false].sample
end
end
class Bus < MotorVehicle
attr_reader :passengers, :stop_requested
def initialize(args={})
@color = args[:color]
@wheels = args[:wheels]
@num_seats = args[:num_seats]
@fare = args[:fare]
@stop_requested = [true,false].sample
@passengers=[]
@gas = [true,true,true,false].sample
end
def admit_passenger(passenger,money)
@passengers << passenger if money > @fare
end
end
class Motorbike < MotorVehicle
attr_reader :stop_requested
def initialize(args={})
@color = args[:color]
@wheels = 2
@status = :stopped
@stop_requested = false
@gas = [true,false,false,false].sample
end
def drive
@status = :driving
@speed = :fast
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
require "rspec"
require_relative "Inheritance"
describe Car do
describe "#need_gas?" do
it "returns true 66% of the time if the car needs gas" do
need_gas_one = [true,true,true].sample
expect(need_gas_one).to eql(true)
end
end
describe "#drive" do
it "increases speed and changes the status to driving if stop is not requested" do
murano = Car.new(color: "black", wheels: 4, stop_requested: false, gas: true)
expect(murano.drive).to eql(:driving)
end
end
describe "#brake" do
it "changes the status of the car to stopped" do
nissan = Car.new(color: "blue", wheels: 4, stop_requested: true, gas: true)
expect(nissan.brake).to eql(:stopped)
end
end
end
describe Bus do
describe "#admit_passenger" do
it "adds a passenger to the vehicle if the passenger has correct fare amount" do
big_yellow = Bus.new(color: "Yellow", wheels: 8, num_seats: 24, fare: "$2.50")
big_yellow.admit_passenger("John Doe", "$3.00")
expect(big_yellow.passengers).to eql(["John Doe"])
end
end
end
describe Motorbike do
describe "#weave_through_traffic" do
it "changes the status of the motorbike to driving like a crazy person" do
harley = Motorbike.new(color: "silver")
expect(harley.weave_through_traffic).to eql(:driving_like_a_crazy_person)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment