Skip to content

Instantly share code, notes, and snippets.

@KevinFalank
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active January 3, 2016 19:59
Show Gist options
  • Save KevinFalank/8511609 to your computer and use it in GitHub Desktop.
Save KevinFalank/8511609 to your computer and use it in GitHub Desktop.
require_relative 'P5: OO Inheritance'
puts "***** CAR *********"
p Car::WHEELS == 4
new_car = Car.new(:color => "Green")
p [true, false].include?(new_car.needs_gas?)
p new_car.drive == :driving
p new_car.brake == :stopped
p new_car.crash == :Oh_my_god_Oh_my_god!
p new_car.turbo_boost == :wheeeeeeeeee!
puts "***** BUS *********"
h = {:color => "Blue",
:wheels => "10",
:num_seats => 40,
:fare => 2.25}
bus = Bus.new(h)
p [:driving, :stopped].include?(bus.drive)
p bus.brake == :stopped
bus.admit_passenger("Kevin",5)
bus.admit_passenger("Fred",3)
p bus.passengers == ["Kevin", "Fred"]
p [true, false].include?(bus.needs_gas?)
p [true, false].include?(bus.stop_requested?)
p bus.crash == :Oh_my_god_Oh_my_god!
# p bus.turbo_boost == :wheeeeeeeeee! #=> undefined method `turbo_boost'
puts "***** MOTORBIKE *********"
bike = Motorbike.new(:color => "Black")
p Motorbike::WHEELS == 2
p bike.drive == :fast
p bike.brake == :stopped
p [true, false].include?(bike.needs_gas?)
p bike.weave_through_traffic == :driving_like_a_crazy_person
p bike.crash == :Oh_my_god_Oh_my_god!
class Vehicle
def initialize(args)
@color = args
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def needs_gas?
return [true,true,false].sample
end
def crash
@status = :Oh_my_god_Oh_my_god!
end
end
class Car < Vehicle
WHEELS = 4
def initialize(args)
super(args[:color])
@wheels = WHEELS
end
def turbo_boost
@status = :wheeeeeeeeee!
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
super(args[:color])
@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
def needs_gas?
return [true,true,true,false].sample
end
end
class Motorbike < Vehicle
WHEELS = 2
def initialize(args)
super(args[:color])
@wheels = WHEELS
end
def drive
super
@speed = :fast
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