Created
December 27, 2017 23:11
-
-
Save onelharrison/107bf748108d9dbecd02c8aaac1c9536 to your computer and use it in GitHub Desktop.
Code snippet showing how to define public and private instance methods in Ruby.
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
class Car | |
attr_reader :mileage | |
def initialize | |
@mileage = 0 | |
end | |
def drive | |
update_mileage | |
end | |
private | |
def update_mileage | |
@mileage += 1 | |
end | |
end | |
# => car = Car.new | |
# #<Car:0x005..> | |
# | |
# => car.drive | |
# 1 | |
# | |
# => car.update_mileage | |
# private method `update_mileage' called for #<Car:0x005..> | |
# | |
# => car.mileage | |
# 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment