Last active
December 7, 2020 21:46
-
-
Save brlafreniere/8d7fd44f46e4150ecdbf1a16d4af66fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car | |
attr_reader :current_speed | |
attr_reader :brand | |
attr_reader :max_speed | |
# use hash args because then you know what parameters are what when calling | |
# the function. | |
def initialize(args = {}) | |
@current_speed = args[:current_speed] || 0 | |
@brand = args[:brand] || "unknown" | |
@max_speed = args[:max_speed] || 0 | |
end | |
def accelerate | |
@current_speed += 1 | |
puts "We're now cruising at: #{@current_speed}" | |
end | |
def drive | |
puts "Buckle up, gramps." | |
puts "Starting speed: #{@current_speed}" | |
while @current_speed < @max_speed | |
self.accelerate | |
sleep 1 | |
end | |
if @current_speed == @max_speed | |
puts "I'm givin' her all she's got captain!" | |
end | |
end | |
end | |
class Camaro < Car | |
def initialize(args = {}) | |
super(args) | |
@brand = args[:brand] || "Chevy" | |
@max_speed = args[:max_speed] || 200 | |
end | |
end | |
c = Camaro.new(current_speed: 190) | |
c.drive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment