2

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.

Joris Ooms
  • 11,880
  • 17
  • 67
  • 124

1 Answers1

3

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)).

Gareve
  • 3,372
  • 2
  • 21
  • 23
  • So how would you go about calling the 'b' method? I'm still confused, but getting closer to understanding it, I think. – Joris Ooms Mar 06 '11 at 22:40
  • i would create another accesible method: public def call_b b() end call_b its inside the class, and have access to the private methods. – Gareve Mar 06 '11 at 22:43
  • Thanks a lot. I understand the concept now but I'm still a bit confused as to why you would use private if it means having to put in another step.. The difference still isn't all too clear to me. Sorry for being such a hassle ;) – Joris Ooms Mar 06 '11 at 23:06
  • 1
    This its more for make your code more organizated. Thats one of the reasons of why the object oriented programming exists. Some methods or atttributes of your class are not be used externally, then why you would make them public (this is more visible in documentation and IDEs with code features similar to 'intellisense'). If You see some random code (i love the openness of ruby) you will see that there are over 9000 methods, but some methods are hidden (encapsulated) because are internal 'helpers' of the public methods. – Gareve Mar 06 '11 at 23:15