# With and without Song#to\_s

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

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

</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?

In that case, the default implementation of to\_s kicks in (defined in  
Object).

–  
James B.

“If you don’t write it down, it never happened.”

- (Unknown)

---

<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:53am UTC](https://www.ruby-forum.com/t/with-and-without-song-to-s/80699/3 "2006-12-30T06:53:11Z")

</div>

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

> > 
> 
> In that case, the default implementation of to\_s  
> kicks in (defined in  
> Object).

I don’t call the method Song.to\_s in my script. Why  
does it kick in?

Thanks,

Li
