Skip to content

Instantly share code, notes, and snippets.

@RainMonster
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 20, 2015 03:59
Show Gist options
  • Select an option

  • Save RainMonster/6067747 to your computer and use it in GitHub Desktop.

Select an option

Save RainMonster/6067747 to your computer and use it in GitHub Desktop.
class Vehicle
attr_accessor :status, :magical_creature
attr_reader :color, :wheels
def initialize(args)
@color = args[:color]
@status = 'stopped'
@wheels = args[:wheels]
@magical_creature = false
end
def brake
@status = 'stopped'
end
def drive
@status = 'driving'
end
def needs_gas?
[true,true,false].sample
end
end
class Car < Vehicle
def initialize(args)
super
@wheels = 4
end
end
### Car tests ###
sexy_car = Car.new({:color => 'dingy tan'})
p sexy_car.drive == 'driving'
p sexy_car.needs_gas?
p sexy_car.color == 'dingy tan'
p sexy_car.brake == 'stopped'
class Bus < Vehicle
attr_reader :wheels, :color, :passengers, :num_seats, :fare
def initialize(args)
super
@wheels = args[:wheels]
@num_seats = args[:num_seats]
@fare = args[:fare]
@passengers=[]
@magical_creature = true
end
def drive
stop_requested? == true ? self.brake : @status = "Racing through the Japanese countryside"
end
def admit_passenger(passenger,money)
@passengers << passenger if money >= @fare
end
def stop_requested?
[true,false].sample
end
def needs_gas?
[true,true,true,false].sample
end
def accept_sad_girls(passenger,fare)
fare == 'corn' ? @passengers << passenger : "MEOWRRRR"
end
end
### Bus tests ###
cat_bus = Bus.new({:color => 'Furry', :wheels => 'legs', :num_seats => 6, :fare => 2.00})
cat_bus.admit_passenger('small totoro', 2.00)
p cat_bus.passengers == ['small totoro']
cat_bus.admit_passenger('small girl', 0)
cat_bus.accept_sad_girls('small girl', 'corn')
p cat_bus.passengers == ['small totoro', 'small girl']
p cat_bus.drive
p cat_bus.drive
p cat_bus.drive
p cat_bus.drive
p cat_bus.wheels == 'legs'
p cat_bus.magical_creature == true
class Motorbike < Vehicle
attr_accessor :speed, :status
def initialize(args)
super
@wheels = 2
end
def drive
super
@speed = 'fast'
end
def needs_gas?
[true,false,false,false].sample
end
def weave_through_traffic
@status = 'driving like a crazy person'
end
end
### Motorbike tests ###
my_cool_bike = Motorbike.new({:color => 'black'})
p my_cool_bike.drive == 'fast'
p my_cool_bike.brake == 'stopped'
p my_cool_bike.weave_through_traffic == 'driving like a crazy person'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment