Access Levels

suggest change

Ruby has three access levels. They are public, private and protected.

Methods that follow the private or protected keywords are defined as such. Methods that come before these are implicitly public methods.

Public Methods

A public method should describe the behavior of the object being created. These methods can be called from outside the scope of the created object.

class Cat
  def initialize(name)
    @name = name
  end

  def speak
    puts "I'm #{@name} and I'm 2 years old"
  end
  
end

new_cat = Cat.new("garfield")
#=> <Cat:0x2321868 @name="garfield">
 
new_cat.speak
#=> I'm garfield and I'm 2 years old

These are public Ruby methods. They describe how a new cat is initialized and how speak behaves.

The public keyword is unnecessary at the start of a class, but it can be used to leave a private or protected section.

class MyClass
  def first_public_method
  end

  private

  def private_method
  end

  public

  def second_public_method
  end
end

Private Methods

Private methods are not accessible from outside of the object. They are used internally by the object. Using the cat example again:

class Cat
  def initialize(name)
    @name = name
  end

  def speak
    age = calculate_cat_age # here we call the private method
    puts "I'm #{@name} and I'm #{age} years old"
  end

  private
  def calculate_cat_age
    2 * 3 - 4
  end
end

my_cat = Cat.new("Bilbo")
my_cat.speak #=> I'm Bilbo and I'm 2 years old
my_cat.calculate_cat_age #=> NoMethodError: private method `calculate_cat_age' called for #<Cat:0x2321868 @name="Bilbo">

As you can see in the example above, the newly created Cat object has access to the calculate_cat_age method internally. We assign the variable age to the result of running the private calculate_cat_age method which prints the name and age of the cat to the console.

When we try to call calculate_cat_age from outside the my_cat object, we receive a NoMethodError because it’s private. Get it?

Protected Methods

Protected methods are very similar to private methods. They cannot be called from outside an instance the way a public method can. Unlike a private method, a protected method can be called with an explicit receiver when that receiver is self or another instance of the same class or subclass.

class Cat
  def initialize(name, age)
    @name = name
    @age = age
  end

  def speak
    puts "I'm #{@name} and I'm #{@age} years old"
  end

  # This == method compares two cats' own ages.
  # If both cats have the same age they are considered equal.
  def ==(other)
     self.own_age == other.own_age
  end

  protected
     def own_age
        @age
     end
end

cat1 = Cat.new("ricky", 2)
# => #<Cat @name="ricky", @age=2>

cat2 = Cat.new("lucy", 4)
# => #<Cat @name="lucy", @age=4>

cat3 = Cat.new("felix", 2)
# => #<Cat @name="felix", @age=2>

You can see we’ve added an age parameter to the cat class and created three new cat objects with a name and an age. We are going to call the protected own_age method to compare the ages of our cat objects.

cat1 == cat2 # => false

cat1 == cat3 # => true

Look at that: inside cat1 we read its age with the protected method self.own_age and compared it with cat2 by calling cat2.own_age.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents