Skip to content

Instantly share code, notes, and snippets.

@SmilinColleen
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 20, 2015 03:48
Show Gist options
  • Save SmilinColleen/6066200 to your computer and use it in GitHub Desktop.
Save SmilinColleen/6066200 to your computer and use it in GitHub Desktop.
Original file with driver code added
class Vehicle
def initialize(args)
@color = args[:color]
end
def brake
@status = :stopped
end
def needs_gas?
puts "not implemented"
end
def drive
@status = :driving
end
def turn_on
@status = :idling
end
def turn_off
if @status == :stopped
"can't turn off car. already off"
else
@status = :stopped
end
end
end
class Car < Vehicle
@@WHEELS = 4
def initialize(args)
super
@wheels = @@WHEELS
end
def needs_gas?
return [true,true,false].sample
end
def open_door
p brake
@status = :door_opened
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
@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 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
@wheels = @@WHEELS
end
def drive
@status = :driving
@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
#Driver Code
car1 = Car.new(color: 'blue')
p car1
p car1.drive
p "The car needs gas: #{car1.needs_gas?}"
p car1.brake
puts
bike = Motorbike.new(color: 'black')
p "Driving #{bike.drive}"
p "The motorbike needs gas: #{bike.needs_gas?}"
p "Weaving through traffice means #{bike.weave_through_traffic}"
puts
bus = Bus.new(color: 'white', wheels: 8, num_seats: 41, fare: 2)
# can add details in parenthesis as hash or put it as variable above it.
# i tried earlier and failed but must have had typoe
p bus
p bus.drive
p bus.drive
p "stop requested: #{bus.stop_requested?}"
p bus.passengers
p bus.inspect
p bus.admit_passenger('Nancy', 10)
p bus.admit_passenger('Jim', 10)
25.times {bus.admit_passenger(1, 40)}
p bus.passengers.length
p bus.admit_passenger("Sally", 13)
p bus.passengers.length
# note can have more passengers than seats since people stand
p "stop requested: #{bus.stop_requested?}"
p "The bus needs gas: #{bus.needs_gas?}"
p bus.brake
p bus.drive
puts "open car door!"
p car1.open_door
p car1.turn_on
p car1.turn_off # stopped
p car1.turn_off # prints "can't turn off car. already off"
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
#Driver Code
car1 = Car.new(color: 'blue')
p car1
p car1.drive
p "The car needs gas: #{car1.needs_gas?}"
p car1.brake
puts
bike = Motorbike.new(color: 'black')
p "Driving #{bike.drive}"
p "The motorbike needs gas: #{bike.needs_gas?}"
p "Weaving through traffice means #{bike.weave_through_traffic}"
puts
bus = Bus.new(color: 'white', wheels: 8, num_seats: 41, fare: 2)
# can add details in parenthesis as hash or put it as variable above it.
# i tried earlier and failed but must have had typoe
p bus
p bus.drive
p bus.drive
p "stop requested: #{bus.stop_requested?}"
p bus.passengers
p bus.inspect
p bus.admit_passenger('Nancy', 10)
p bus.admit_passenger('Jim', 10)
25.times {bus.admit_passenger(1, 40)}
p bus.passengers.length
p bus.admit_passenger("Sally", 13)
p bus.passengers.length
# note can have more passengers than seats since people stand
p "stop requested: #{bus.stop_requested?}"
p "The bus needs gas: #{bus.needs_gas?}"
p bus.brake
p bus.drive
@zachgersh
Copy link

Hey @SmilinColleen,

Nice job on the refactor! Couple of things, did you consider killing the class variable for wheels? Seems like it is ripe for turning into an instance variable of each vehicle.

needs_gas is also a method that seems to differ a bit between the classes but it also felt like it was cool to move that up into vehicle.

Finally, did you consider turning vehicle into a module?

Finally, sparkles ✨

@SmilinColleen
Copy link
Author

I actually have a version on my computer with vehicle as a module but then didn't upload it. was debating uploading it. I thought the assignment was to make it all in classes so then I didn't upload it. But perhaps I should show it too.

I was looking at the needs_gas and didn't like how the true/false samples were all different. I was considering doing something about that but I guess I forgot. I totally didn't think about wheels at all. I should update that. Thanks. I spent too much time with my mind wandering on the "be creative" part to add things to the Vehicle class and 1 other class. I had trouble thinking of what to add (even though I know that shouldn't have been the tough part of the assignment.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment