Skip to content

Instantly share code, notes, and snippets.

@onelharrison
Created December 27, 2017 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onelharrison/107bf748108d9dbecd02c8aaac1c9536 to your computer and use it in GitHub Desktop.
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.
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