# Access Modifiers

**URL:** <https://www.ruby-forum.com/t/access-modifiers/105689>\
**Category:** Ruby\
**Created:** [June 1, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/access-modifiers/105689 "2007-06-01T12:33:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vineetraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c57346/32.png) [@vineetraja](https://www.ruby-forum.com/u/vineetraja)\
**Post date:** [June 1, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/access-modifiers/105689/1 "2007-06-01T12:33:03Z")

</div>

Hi All  
Now another hitch:

1 class Person  
2 attr\_reader :genre, :style  
3 protected :genre  
4 private :to\_s  
5 def psedonym  
6 @name  
7 end  
8  
9 def to\_s  
10 “just another person”  
11 end  
12 #private :to\_s  
13 end  
14  
15 p= Person.new  
16 puts p  
17 puts “\*\*\*\*\*\*\*\*”  
18 puts p.to\_s

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
CASE I  
With the above class the output comes out to be:

> ruby yet.rb  
> just another person

* * *

just another person

CASE II  
Now when I comment Line 4 and Uncomment Line 12, the output comes to be:

> ruby yet.rb  
> just another person

* * *

yet.rb:18: private method `to\_s’ called for just another person:Person  
(NoMethodError)  
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

THE HITCH

In first case it appears that that private declaration had no effect,  
thus the textual position matters here; If this is really so, can anyone  
explain why??

AND

In second case why the private declaration had no effect on  
puts p  
when in fact (this is what i infer) it uses the same method which  
puts p.to\_s  
uses.

Can anyone Explain this bias???

Thanks  
Raja

---

<div class="post-metadata">

**Author:** ![vineetraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c57346/32.png) [@vineetraja](https://www.ruby-forum.com/u/vineetraja)\
**Post date:** [June 1, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/access-modifiers/105689/2 "2007-06-01T14:00:53Z")

</div>

On 6/1/07, Vin R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> THE HITCH
> 
> In first case it appears that that private declaration had no effect,  
> thus the textual position matters here; If this is really so, can anyone  
> explain why??

Note first that private is not a declaration.  
It is a method call.

In your case, note that to\_s is actually defined on Object already.  
In case 1, the to\_s inherited from Object is turned private, then gets  
overridden.  
When you override it, it is no longer private.

In case 2, you override to\_s first and then make it private, leaving it  
private.

Keep in mind that this happens because you are overriding an existing  
method!  
You would get different behaviour if it wasn’t an existing method  
Play around in irb more 🙂

> AND
> 
> In second case why the private declaration had no effect on  
> puts p  
> when in fact (this is what i infer) it uses the same method which  
> puts p.to\_s  
> uses.

p doesn’t call to\_s. If you look at the docs, it actually calls  
Object#inspect  
which then uses to\_s → which is OK, because they are methods on the  
same object!

Kernel.p docs:  
[http://corelib.rubyonrails.org/classes/Kernel.html#M002080](http://corelib.rubyonrails.org/classes/Kernel.html#M002080)

---

<div class="post-metadata">

**Author:** ![vineetraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c57346/32.png) [@vineetraja](https://www.ruby-forum.com/u/vineetraja)\
**Post date:** [June 1, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/access-modifiers/105689/3 "2007-06-01T14:59:00Z")

</div>

On 2007-06-01 19:33:03 +0900 (Fri, Jun), Vin R. wrote:

> 8
> 
> Now when I comment Line 4 and Uncomment Line 12, the output comes to be:  
> In first case it appears that that private declaration had no effect,
> 
> Can anyone Explain this bias???

While waiting for someone wiser than me to answer, I’ll try to explain  
how I understand it 🙂

In line 4 you change the visibility of existing method to\_s.  
In line 9 you declare new method with _public_ visibility.  
That’s why the line 12 is required to change it back to private.

And why? That’s because in Ruby the definition of your class is not  
parsed and then compiled in one step, just it is executed as ordinary  
program - each line in your code does something. That’s why the order of  
your lines matter - it matters if you first change the visibility of  
the method and then define it.

Try to do something like this:

class A  
private :non\_existing  
def non\_existing  
p ‘in non\_existing’  
end  
end

class B  
def non\_existing  
p ‘in non\_existing’  
end  
private :non\_existing  
end

In your second topic, the private method to\_s may be invoked by not  
calling it directly, but by using the ‘send’ hack:  
The actual command executed by ‘puts’ is not ‘object.to\_s’ but  
‘object.send(:to\_s)’.

Try something like:

class Person  
def to\_s  
‘bleh’  
end  
private :to\_s  
end

puts Person.new.to\_s  
=\> NoMethodError: private method `to\_s’ called for bleh:Person

puts Person.new.send(:to\_s)  
=\> “bleh”

The ‘send’ hack allows you to call private methods of any object. To  
prevent this you could:

- make ‘send’ private  
(not recommended, since this would bleak almost everything, I’m  
afraid)
- override ‘send’ and react when it receives :to\_s as parameter.

HTH.
