Created
March 9, 2016 19:36
-
-
Save FaisalAl-Tameemi/bbbcd89370e8844bf7fa to your computer and use it in GitHub Desktop.
Classes review + Module example
This file contains hidden or 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
require_relative('./car_methods') | |
class Car | |
attr_accessor :current_speed | |
def initialize(model, color) | |
@model = model | |
@color = color | |
@current_speed = 0 | |
end | |
include CARMETHODS | |
end |
This file contains hidden or 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
module CARMETHODS | |
def drive_forward | |
self.current_speed += 50 | |
end | |
def drive_reverse | |
self.current_speed -= 20 | |
end | |
end |
This file contains hidden or 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
require_relative('./car') | |
require_relative('./truck') | |
prius = Car.new("Toyota Prius", "Light Brown") | |
prius.drive_forward | |
puts "The Prius is going at a speed of #{prius.current_speed}" | |
f150 = Truck.new("Ford F150", "Dark Blue") | |
f150.drive_forward | |
f150.drive_forward | |
puts "The F150 is going at a speed of #{f150.current_speed}" |
This file contains hidden or 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
require_relative('./car_methods') | |
class Truck | |
attr_accessor :current_speed | |
def initialize(model, color) | |
@model = model | |
@color = color | |
@current_speed = 0 | |
end | |
include CARMETHODS | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment