Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4019296
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:05:14+00:00 2026-05-20T10:05:14+00:00

I’m reading through Beginning Ruby and I’m stuck at the part about private and

  • 0

I’m reading through Beginning Ruby and I’m stuck at the part about private and protected methods. This is a newbie question, I know. I searched through SO for a bit but I couldn’t manage to find a clear and newbie-friendly explanation of the difference between private and protected methods.

The book gives two examples, the first one for private methods:

class Person
  def initialize(name)
    set_name(name)
  end

  def name
    @first_name + ' ' + @last_name
  end

  private
  def set_name(name)
    first_name, last_name = name.split(/\s+/)
    set_first_name(first_name)
    set_last_name(last_name)
  end

  def set_first_name(name)
    @first_name = name
  end
  def set_last_name(name)
    @last_name = name
  end
end

In this case, if I try

 p = Person.new("Fred Bloggs") 
 p.set_last_name("Smith")

It will tell me that I can’t use the set_last_name method, because it’s private. All good till there.

However, in the other example, they define an age method as protected and when I do

 fred = Person.new(34)
 chris = Person.new(25)
 puts chris.age_difference_with(fred)
 puts chris.age

It gives an error:

:20: protected method 'age' called for #<Person:0x1e5f28 @age=25> (NoMethodError)

I honestly fail to see the difference between the private and protected methods, it sounds the same to me. Could someone provide me with a clear explanation so I’ll never get confused about this again?

I’ll provide the code for the second example if necessary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:05:15+00:00Added an answer on May 20, 2026 at 10:05 am

    protected methods (or atributes) can only be used by the classes that inherit the class with protected methods (or atributes).

    a      d
     \      \
      b      e
       \
        c
    

    If the class a have a protected method, this can be used by b and c, but can’t be used by d or e.
    Note: Ascii art diagram for inherit in ruby classes.

    class A   
       public 
       def f obj
          obj.c
       end
       def g obj
          obj.b
       end
       def call_b
         b
       end
    
       private
    
       def b
          puts "Hi!_b"
       end
    
    
       protected
       def c
          puts "Hi!_c"
       end
    end
    
    a = A.new
    b = A.new
    
    a.f(b) # Hi!_c
    a.g(b) # inj.rb:7:in `g': private method `b' called for #<A:0xb76df4cc> (NoMethodError)
    a.call_b # Hi!_b  
    

    In this case, the method f can ‘see’ the protected method because its of the same class (or an inherited one), but the private method encapsulate(hides) the ‘b’ method of all the cases, except if this is called inside of his class (by another accesible method (in this case, the method call_b)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.