All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

How self, protected, and private work (Ruby)

Post cover: How self, protected, and private work (Ruby)
This content has been automatically translated from Ukrainian.
In Ruby, it is easy to get confused when it comes to access levels for methods - especially in cases involving self. At first glance, protected and private seem similar, but the difference manifests in how Ruby interprets calls through self.
In Ruby, any method can be called in two ways:
do_something      # without self
self.do_something # with self
public - methods are always accessible
protected - accessible only for objects of the same class or subclass
private - accessible only without an explicit receiver (self)

private - cannot be called through self

Private methods are methods that can only be called "from within themselves". Ruby does not allow specifying a receiver (self), even if it is the same object.
class User
  def greet
    self.secret_message  # cannot
  end

  def greet_ok
    secret_message       # can
  end

  private

  def secret_message
    "Hello from private"
  end
end

user = User.new
user.greet_ok # => "Hello from private"
user.greet    # => NoMethodError
private restricts calls only in the context of the current object, without explicit self.

protected - can be called through self, but only among objects of the same class

Unlike private, protected methods allow calls through self or another instance, as long as they belong to the same class or subclass.
class User
  def initialize(age)
    @age = age
  end

  def older_than?(other)
    self.age > other.age  # can through self
  end

  protected

  def age
    @age
  end
end

u1 = User.new(30)
u2 = User.new(20)

u1.older_than?(u2) # => true
protected is often used when you need to compare or interact between instances of the same class, but not provide access to external code.

public - without restrictions

Public methods are always accessible, both with self and without it.
class User
  def greet
    self.name
  end

  def name
    "Marian"
  end
end

User.new.greet # => "Marian"

In summary

  • public - methods are accessible to everyone.
  • protected - methods can be called through self and between instances of the same class.
  • private - methods can only be called without self, within the current object.
To remember all this - create an rb file locally and experiment with the code. Only through practice can you truly grasp the difference.

This post doesn't have any additions from the author yet.

How arrays work in Ruby: practical examples of each, map, select, inject, reduce, filter_map
28 Oct 10:38

How arrays work in Ruby: practical examples of each, map, select, inject, reduce, filter_map

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Integer division in Ruby: why 6 / 4 equals 1
28 Oct 14:10

Integer division in Ruby: why 6 / 4 equals 1

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
28 Oct 14:42

How &:to_s works in Ruby and what Symbol#to_proc is

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What are Proc and Lambda in Ruby?
28 Oct 15:57

What are Proc and Lambda in Ruby?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What happens if you call [1, 2, 3].map(&Person)
29 Oct 17:54

What happens if you call [1, 2, 3].map(&Person)

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Singleton class (eigenclass) in Ruby: what it is and why it is needed
29 Oct 18:29

Singleton class (eigenclass) in Ruby: what it is and why it is needed

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
==, equal?, eql?, === in Ruby: what they check and when to use them
29 Oct 20:47

==, equal?, eql?, === in Ruby: what they check and when to use them

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska