# Protected methods: not clear to me and highly confusing

**URL:** <https://www.ruby-forum.com/t/protected-methods-not-clear-to-me-and-highly-confusing/111376>\
**Category:** Rails\
**Created:** [August 6, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/protected-methods-not-clear-to-me-and-highly-confusing/111376 "2007-08-06T17:06:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [August 6, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/protected-methods-not-clear-to-me-and-highly-confusing/111376/1 "2007-08-06T17:06:31Z")

</div>

class Person

```
def initialize(name,age)
    @name,@age = name,age
end

def <=>(other)
    age <=> other.age
end

attr_reader :name,:age
protected :age

```

end

p1 = Person.new(“Fred”,21)  
puts p1.age

That causes an error:  
NoMethodError: protected method `age’ called for #\<Person:0x2dc876c  
@age=31,  
@name=“fred”\>  
from (irb):41

However check this one out.  
p2 = Person.new(“John”,23)  
compage = (p1 \<=\> p2)

This one works.

My question is how come “other.age” works however “p1.age” doesn’t . I  
guess  
it has something to do with scope. Can someone put the explanation in an  
easy to understand  
sentence.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [August 6, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/protected-methods-not-clear-to-me-and-highly-confusing/111376/2 "2007-08-06T17:11:32Z")

</div>

That’s how ruby works.  
[http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby](http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby)

This is a ruby-talk question 🙂

On 8/6/07, Neeraj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> attr_reader :name,:age
> 
> ```
> 
> sentence.
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [August 6, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/protected-methods-not-clear-to-me-and-highly-confusing/111376/3 "2007-08-06T19:29:54Z")

</div>

First off you are giving Ruby some confused signals.

attr\_reader says to creat public accesor age. Then you declare age as  
protected. Protected methods are not to be accessed outside the class.  
Do not define age method protected.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [August 6, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/protected-methods-not-clear-to-me-and-highly-confusing/111376/4 "2007-08-06T19:29:54Z")

</div>

Thanks Pratik but Jamis article didn’t elaborate much. At least not for  
me.

- Neeraj

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [August 6, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/protected-methods-not-clear-to-me-and-highly-confusing/111376/5 "2007-08-06T19:44:58Z")

</div>

Hi –

On Mon, 6 Aug 2007, Neeraj K. wrote:

> attr\_reader :name,:age
> 
> However check this one out.  
> p2 = Person.new(“John”,23)  
> compage = (p1 \<=\> p2)
> 
> This one works.
> 
> My question is how come “other.age” works however “p1.age” doesn’t . I guess  
> it has something to do with scope. Can someone put the explanation in an  
> easy to understand sentence.

At the time you call a protected method on x, self must be an instance  
of the same class (or a subclass) as x.

That is true inside \<=\>, where both self and other are of class  
Person. But it’s not true at the top level, where p1 is a Person but  
self is the Object instance main (the top-level default object).

You should, as another poster suggested, have a look at the ruby-talk  
list if you have Ruby-specific questions – not because they’re not  
directly relevant to Rails development but because they’ll fit in  
better there and probably get more response overall.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
