# Puts bypasses access restrictions on to\_s?

**URL:** <https://www.ruby-forum.com/t/puts-bypasses-access-restrictions-on-to-s/100452>\
**Category:** Ruby\
**Created:** [July 29, 2006, 12:16am UTC](https://www.ruby-forum.com/t/puts-bypasses-access-restrictions-on-to-s/100452 "2006-07-29T00:16:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [July 29, 2006, 12:16am UTC](https://www.ruby-forum.com/t/puts-bypasses-access-restrictions-on-to-s/100452/1 "2006-07-29T00:16:05Z")

</div>

I read somewhere that “puts” calls “to\_s” on an object to get a string  
representation and then displays that. This seems to be correct in my  
experience.

My question is: how does puts bypass “private” restrictions to get the  
value of to\_s?

See here:

irb(main):005:0\> class RiseAndShine  
irb(main):006:1\> private  
irb(main):007:1\> def to\_s  
irb(main):008:2\> “rising and shining”  
irb(main):009:2\> end  
irb(main):010:1\> end  
=\> nil  
irb(main):011:0\> rs = RiseAndShine.new  
=\> rising and shining  
irb(main):012:0\> rs.to\_s  
NoMethodError: private method `to\_s’ called for rising and  
shining:RiseAndShine  
from (irb):12  
irb(main):013:0\> puts rs  
rising and shining  
=\> nil  
irb(main):014:0\>

---

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [July 29, 2006, 12:36am UTC](https://www.ruby-forum.com/t/puts-bypasses-access-restrictions-on-to-s/100452/2 "2006-07-29T00:36:27Z")

</div>

On Sat, 29 Jul 2006, Leslie V. wrote:

> =\> rising and shining  
> irb(main):012:0\> rs.to\_s  
> NoMethodError: private method `to\_s’ called for rising and  
> shining:RiseAndShine  
> from (irb):12  
> irb(main):013:0\> puts rs  
> rising and shining

irb(main):008:0\> rs. **send** (:to\_s)  
=\> “rising and shining”

Ruby lets private be circumvented.

To see how puts really works, though, take a look at the source code.  
It’s pretty easy to read. Look in io.c for rb\_io\_puts(). Start from  
there.

Kirk H.

---

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [July 29, 2006, 12:39am UTC](https://www.ruby-forum.com/t/puts-bypasses-access-restrictions-on-to-s/100452/3 "2006-07-29T00:39:59Z")

</div>

On 28/07/06, Leslie V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I read somewhere that “puts” calls “to\_s” on an object to get a string  
> representation and then displays that. This seems to be correct in my  
> experience.
> 
> My question is: how does puts bypass “private” restrictions to get the  
> value of to\_s?

Interesting question. I had a look at the C source, and it uses  
rb\_funcall, which doesn’t check private/protected, to call to\_s.

If you want to do the same thing, you can use this:  
rs. **send** (:to\_s)

Paul.

---

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [July 29, 2006, 11:22am UTC](https://www.ruby-forum.com/t/puts-bypasses-access-restrictions-on-to-s/100452/4 "2006-07-29T11:22:25Z")

</div>

On 7/29/06, Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you want to do the same thing, you can use this:  
> rs. **send** (:to\_s)

That seems wrong! Not that you can bypass access restrictions, which I  
think you can do in many languages, but that ‘puts’ does so. Though  
OTOH I’m sure there’s a very good argument for ‘puts’ being  
high-speed.
