Public, Private and Protected
Methods are public by default. private hides them from outside callers, and protected allows other instances of the same class to call them.
Example
class Account
def initialize(b) = @balance = b
def bigger?(o) = balance > o.balance
protected
def balance = @balance
private
def secret = "hidden"
end
puts Account.new(5).bigger?(Account.new(3))
begin; Account.new(1).secret; rescue NoMethodError => e; puts "private!"; endResult
true private!
Exercise
Hide a method.
def secret; endQuiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.