Skip to content
Justice Tech School

public, private, protected

private methods are only callable inside the object. protected allows other instances of the class.

Example
class A
  def run = helper
  private
  def helper = "secret"
end
puts A.new.run
begin
  A.new.helper
rescue NoMethodError
  puts "private"
end

Result

secret
private

Exercise

Hide a method.


def x; end

Quiz Test

Can you call private methods from outside?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.