Share this post!

Public, Private, Protected: Understanding Method Visibility In Ruby

What does public, private & protected mean in Ruby?

These 3 methods control the public interface of your class.

They control WHO can call these methods.

By default all your methods are public, anyone can use them.

But you can restrict them to internal use only.

Why would you want to do that?

To make these internal methods easier to change later.

Imagine you’re writing a code library that is going to be used in a few different projects at work.

When these projects implement your library they’re going to be calling methods on the classes that you’ve created.


You release a new version…

But you decided to change the name on a public method.

That’s going to produce errors on every project that is using this method!

By having LESS public methods you INCREASE the freedom of change inside your class.

Let’s see some code examples.

Public Visibility

Public is the default method visibility in Ruby.

Here’s an example:

def orange
  "Vitamin C"

If you have an object food that defines orange, you can call it like this:

If a method has been made private or protected, you can make it public again.

Like this:

public :orange

Ruby Private Method

If you’ve ever seen the “private method called” error message then you’ve tried to use a private method.

You can only use a private method directly.

Like this:

puts 123

Without an object, like this:

self.puts 123

# NoMethodError: private method `puts' called

It’s the same method, but because it’s declared as private you can only call it directly.

You can only use private methods:

  • Inside the class that defines them
  • Inside methods inherited from the parent class
  • Inside methods included from a module

In other words, you can’t call private methods from outside the class that defines them.

How do you define a private method?

Like this:

def bacon
  "private bacon"

private :bacon

If you want to declare multiple methods as private…

Do this:

class Food
  def public_method


  def bacon

  def orange

  def coconut

Every method after private becomes a private method.

It’s a common pattern to define all your public methods first, then define your private methods together at the end of the class.

Ruby Protected Method

Protected methods are way more rare…

They are like private methods, but you can call them on an object & not just directly.

If you try this example with private you’ll get an error:

class Food
  def initialize(name)
    @name = name

  def ==(other)
    name ==


  attr_reader :name

food ="chocolate")

puts food == food

You get the error because name would be private so you can’t do

But with protected this code works!

Private vs Protected

That’s the difference, the fact that protected keeps the method private, but it also allows you to call that method on an object.

With private you can only do name, with protected you can do

When should you use protected?

Only if you have a very specific case, like the equals (==) method.

The Ruby documentation recommends using private instead of protected whenever possible.

And there’s this note:

“Note that a protected method is slow because it can’t use inline cache.”

I was curious about this so I ran some benchmarks:

public:    2813891.9 i/s
private:   2699273.8 i/s
protected: 2572122.0 i/s

That’s a difference of 8.5% in performance.

Video Tutorial


You’ve learned about Ruby method visibility, public, private & protected methods. These aren’t Ruby keywords, they are methods themselves defined on the Module class.

Please share this post so more people can understand this topic!

Thanks for reading 🙂

Matej says 7 months ago

I think you made a typo here:
“The Ruby documentation recommends using private instead of private whenever possible.”
You probably thought “instead of protected”.

Nice article btw, keep up the good work!

    Jesus Castello says 7 months ago

    You’re correct! Thank you, it should be fixed now 🙂

Comments are closed