# With and without Song#to\_s again

**URL:** <https://www.ruby-forum.com/t/with-and-without-song-to-s-again/80722>\
**Category:** Ruby\
**Created:** [December 30, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s-again/80722 "2006-12-30T17:10:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 30, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s-again/80722/1 "2006-12-30T17:10:08Z")

</div>

Hi all,

I posted a similar question a short time ago. But I  
don’t think the replies help me understand the  
behaviors. So I post a short version( sorry if it  
bothers you again).

I have the following script. I don’t call Song#to\_s in  
my script but I can print all the attributes of a  
song. Why is that?

Thanks,

Li

#########  
class Song  
def initialize(name,artist,duration)  
@name=name  
@artist=artist  
@duration=duration  
end  
#attr\_reader :name, :artist, :duration

def to\_s  
“#@name\t#@artist\t\t#@duration”  
end  
end

s=Song.new(‘song1’,‘author1’,‘20’)  
puts s

######output

> ruby ruby20c.rb  
> song1 author1 20

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 30, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s-again/80722/2 "2006-12-30T17:16:11Z")

</div>

chen li wrote:

> “#@name\t#@artist\t\t#@duration”
> 
> > Exit code: 0  
> > As s is not a String (that is not s.is\_a? String), it is converted  
> > into string by puts. That exactly means your method to\_s is called for  
> > doing so.

```
Vince
```

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 30, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s-again/80722/3 "2006-12-30T17:42:20Z")

</div>

chen li wrote:

> I have the following script. I don’t call Song#to\_s in  
> my script but I can print all the attributes of a  
> song. Why is that?

chen li, you can find this answer yourself.

1. Open a console and type:  
ri Kernel.puts

2. See that it says that it’s the same as $stdout.puts. Type:  
ri IO.puts

3. See that it says that it does the same thing as print does. Type:  
ri IO.print

4. See that it tells you that it calls the to\_s method of any objects  
that aren’t strings.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 30, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s-again/80722/4 "2006-12-30T18:54:37Z")

</div>

— Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

you can find this answer yourself.

> 1. See that it tells you that it calls the to\_s  
> method of any objects  
> that aren’t strings.

Thank you for the detail explanation. Now I understand  
why #to\_s is called.

Li
