# With and without Song#to\_s

**URL:** <https://www.ruby-forum.com/t/with-and-without-song-to-s/102632>\
**Category:** Ruby\
**Created:** [December 30, 2006, 2:28am UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s/102632 "2006-12-30T02:28:41Z")\
**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, 2:28am UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s/102632/1 "2006-12-30T02:28:41Z")

</div>

Happy holiday for all!

I have two classes (most of them copied from Pickaxe  
2)and they work fine. But I get the address for  
each object if I comment out method Song#to\_s. How to  
explain them?

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#@duration”  
end  
end

class Songlist  
def initialize  
@songs=Array.new  
end

def append(song)  
@songs.push(song)  
#self  
end

def all\_songs  
@songs.each{|song| song}  
end  
end

# populate the Songlist from a file

list=Songlist.new  
File.open(‘song.txt’).each\_line do |line|  
if line=~/\w+/ #skip empty line  
name,artist,duration=line.split(’,’)  
s=Song.new(name,artist,duration)  
list.append(s)  
end  
end

puts" list all songs"  
puts list.all\_songs  
puts

## define Song#to\_s and the screen output

ist all songs  
song1 author1 20  
song8 author1 4  
song5 author2 6  
song7 author1 8  
song2 author5 1  
song4 author7 3  
song6 author3 5  
song8 author6 7

###comment out Song#to\_s and the screen output  
list all songs  
#Song:0x28ab39c  
#Song:0x28ab144  
#Song:0x28ab090  
#Song:0x28aade8  
#Song:0x28aac44  
#Song:0x28aaa8c  
#Song:0x28aa9b0  
#Song:0x28aa514

---

<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, 2:40am UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s/102632/2 "2006-12-30T02:40:45Z")

</div>

chen li wrote:

> Happy holiday for all!
> 
> I have two classes (most of them copied from Pickaxe  
> 2)and they work fine. But I get the address for  
> each object if I comment out method Song#to\_s. How to  
> explain them?
> 
> T  
> puts calls to\_s on each object that it writes. If you don’t supply a  
> to\_s method for your class then the to\_s method in your class’s  
> superclass will be used. Since the superclass is Object, the Object#to\_s  
> method is used. This version of to\_s simply formats the object’s class  
> and its id as a string.

ri Object#to\_s for more information.

---

<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, 3:04am UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s/102632/3 "2006-12-30T03:04:49Z")

</div>

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

> ri Object#to\_s for more information.

What I don’t understand is that I don’t call Song#to\_s  
in my script why does it affect my codes?

Thanks,

Li

---

<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, 3:12am UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s/102632/4 "2006-12-30T03:12:50Z")

</div>

chen li wrote:

> > and its id as a string.
> > 
> > ri Object#to\_s for more information.
> 
> What I don’t understand is that I don’t call Song#to\_s  
> in my script why does it affect my codes?

puts calls to\_s
