# Listing an Array created within a Class

**URL:** <https://www.ruby-forum.com/t/listing-an-array-created-within-a-class/169173>\
**Category:** Ruby\
**Created:** [May 30, 2009, 7:47pm UTC](https://www.ruby-forum.com/t/listing-an-array-created-within-a-class/169173 "2009-05-30T19:47:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianunderwater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@adrianunderwater](https://www.ruby-forum.com/u/adrianunderwater)\
**Post date:** [May 30, 2009, 7:47pm UTC](https://www.ruby-forum.com/t/listing-an-array-created-within-a-class/169173/1 "2009-05-30T19:47:27Z")

</div>

I am learning from Programming Ruby wich comes with Ruby instalation.  
In the chapter about containers - Implementing a SongList Container  
I took the code in NetBeans like follows:

require ‘Baza.rb’ #here is my Song class  
class SongList \< Song  
def initialize  
@songs = Array.new  
end  
end

class SongList  
def append(aSong)  
@songs.push(aSong)  
self  
end  
end

list = SongList.new  
list.  
append(Song.new(‘title1’, ‘artist1’, 1)).  
append(Song.new(‘title2’, ‘artist2’, 2)).  
append(Song.new(‘title3’, ‘artist3’, 3)).  
append(Song.new(‘title4’, ‘artist4’, 4))

############# Here is my problem  
puts list.to\_a ???  
What line must I have here to have at terminal the array that is created  
in the SongList like this:  
Song: title1–artist1 (1)  
Song: title2–artist2 (2)… etc , so that I can work with it?  
and/or to see the list elements with list[0, 2]  
Thanks

---

<div class="post-metadata">

**Author:** ![adrianunderwater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@adrianunderwater](https://www.ruby-forum.com/u/adrianunderwater)\
**Post date:** [May 30, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/listing-an-array-created-within-a-class/169173/2 "2009-05-30T20:40:37Z")

</div>

list.each

---

<div class="post-metadata">

**Author:** ![adrianunderwater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@adrianunderwater](https://www.ruby-forum.com/u/adrianunderwater)\
**Post date:** [May 30, 2009, 8:48pm UTC](https://www.ruby-forum.com/t/listing-an-array-created-within-a-class/169173/3 "2009-05-30T20:48:09Z")

</div>

Adrian A. wrote:

> I am learning from Programming Ruby wich comes with Ruby instalation.  
> In the chapter about containers - Implementing a SongList Container  
> I took the code in NetBeans like follows:
> 
> require ‘Baza.rb’ #here is my Song class  
> class SongList \< Song  
> def initialize  
> @songs = Array.new  
> end  
> end
> 
> class SongList  
> def append(aSong)  
> @songs.push(aSong)  
> self  
> end  
> end
> 
> list = SongList.new  
> list.  
> append(Song.new(‘title1’, ‘artist1’, 1)).  
> append(Song.new(‘title2’, ‘artist2’, 2)).  
> append(Song.new(‘title3’, ‘artist3’, 3)).  
> append(Song.new(‘title4’, ‘artist4’, 4))
> 
> ############# Here is my problem  
> puts list.to\_a ???

class SongList  
def to\_a  
@songs  
end  
end

puts list.to\_a

OR:

class SongList  
attr\_accessor :songs  
end

puts list.songs

OR: another way is to make your SongList class duck-type like an Array

class SongList  
def   
@songs[elem]  
end  
def []=(elem,obj)  
@songs[elem] = obj  
end

# … etc

end

---

<div class="post-metadata">

**Author:** ![adrianunderwater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@adrianunderwater](https://www.ruby-forum.com/u/adrianunderwater)\
**Post date:** [May 31, 2009, 7:09am UTC](https://www.ruby-forum.com/t/listing-an-array-created-within-a-class/169173/4 "2009-05-31T07:09:16Z")

</div>

Thank you Brian very much, it works!  
Hmmm, how stupid I am. I have learned about this in the previous lesson.  
That’s the begining, but it’s very well that is this forum.

Adrian
