Skip to content

Instantly share code, notes, and snippets.

@georgepradhan
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 20, 2015 03:49
Show Gist options
  • Save georgepradhan/6066542 to your computer and use it in GitHub Desktop.
Save georgepradhan/6066542 to your computer and use it in GitHub Desktop.
# Part 1 tests
car = Car.new({:color => 'red'})
p car.drive == :driving # => true
p car.brake == :stopped # => true
p car.instance_variables.sort == [:@color, :@wheels, :@status].sort
p (car.needs_gas? == true || car.needs_gas? == false) #this sometimes yields false, strangely..
p [true, false].include?(car.needs_gas?) #seems to work
car.needs_gas? #seems to always return true or false, so i'm not sure what's going on above..
# Part 1 tests
puts
bus = Bus.new({ :color => 'blue', :wheels =>4, :num_seas => 25, :fare => 1.5 })
p bus.brake == :stopped # => true
bus.admit_passenger("George", 2)
bus.admit_passenger("John", 1)
p bus.passengers == ["George"]
# Part 1 tests
puts
motorbike = Motorbike.new({:color => 'yellow'})
p motorbike.instance_variable_get(:@wheels) == 2
p motorbike.brake == :stopped
p motorbike.weave_through_traffic == :driving_like_a_crazy_person
class Vehicle
attr_reader :speed
WHEELS = { 'Car' => 4,
'Bus' => 8,
'Motorbike' => 2 }
GAS_PROBABILITY = { 'Car' => [true,true,false],
'Bus' => [true,true,true,false],
'Motorbike' => [true,false,false,false] }
SPEED_LIMIT = 65
def initialize(args)
raise ArgumentError, "Must be initialized with a Hash containing at least a :colors key." unless args[:color]
@color = args[:color]
@wheels = WHEELS[self.class.to_s] || 4
@speed = 0
@speeding_tickets = 0
end
def drive
@status = :driving
@speed = 50 + rand(50) #returns between 50 and 99
gets_a_ticket
end
def brake
@status = :stopped
end
def needs_gas?
GAS_PROBABILITY[self.class.to_s].sample
end
private
def speeding?
@speed > SPEED_LIMIT
end
def gets_a_ticket
if speeding? && [true, false, false].sample
puts "You were going #{self.speed} and the cops got you!"
@speeding_tickets += 1
else
puts "Driving at #{self.speed}"
end
end
end
class Car < Vehicle
end
car = Car.new({:color => 'red'})
# Part 1 tests
puts "----Testing car----"
p car.brake == :stopped # => true
p car.instance_variable_get(:@wheels) == 4 # => true
p [true, false].include?(car.needs_gas?) # => true
puts
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
super
raise ArgumentError, "Must be initialized with a Hash containing :num_seats and :fare keys." unless args[:num_seats] && args[:fare]
@num_seats = args[:num_seats]
@fare = args[:fare]
@passengers=[]
end
def drive
super
self.brake if stop_requested?
end
def admit_passenger(passenger,money)
@passengers << passenger if money > @fare
end
def stop_requested?
[true,false].sample
end
end
bus = Bus.new({ :color => 'blue', :num_seats => 25, :fare => 1.5 })
# Part 1 tests
puts "----Testing bus----"
p bus.brake == :stopped # => true
bus.admit_passenger("George", 2)
bus.admit_passenger("John", 1)
p bus.passengers == ["George"] # => true
puts
class Motorbike < Vehicle
def pop_a_wheely
puts "You sure?"
if gets.chomp.downcase == 'yes'
puts "OK here we go..."
self.drive
end
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
motorbike = Motorbike.new({:color => 'yellow'})
puts "----Testing motorbike----"
p motorbike.instance_variable_get(:@wheels) == 2 # => true
motorbike.drive
p motorbike.brake == :stopped # => true
p motorbike.weave_through_traffic == :driving_like_a_crazy_person
p motorbike.pop_a_wheely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment