# Noob question, regarding array

**URL:** <https://www.ruby-forum.com/t/noob-question-regarding-array/170184>\
**Category:** Ruby\
**Created:** [June 13, 2009, 11:40am UTC](https://www.ruby-forum.com/t/noob-question-regarding-array/170184 "2009-06-13T11:40:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ec9cab/32.png) [@thiago](https://www.ruby-forum.com/u/thiago)\
**Post date:** [June 13, 2009, 11:40am UTC](https://www.ruby-forum.com/t/noob-question-regarding-array/170184/1 "2009-06-13T11:40:37Z")

</div>

I’m a noob, trying to learn ruby.  
I’m following along with the book Programming Ruby, The Pragmatic  
Programmer’s Guide, which comes with ruby documentation.

Im on chapter, Implementing a SongList Container, doing the code  
samples, but i’m getting the following error when trying to run the  
code:  
18:in `append': undefined method`push’ for nil:NilClass  
(NoMethodError)  
from F:/InstantRails/rails\_apps/test/test2.rb:80

I have a SongList class with the following method:

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

and im trying to run the code sample as in the book:

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))

This doesnt make any sense. I can create the Song object just fine,  
but if i try to add it to the array i get this erro.  
Can anyone help me please?

---

<div class="post-metadata">

**Author:** ![thiago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ec9cab/32.png) [@thiago](https://www.ruby-forum.com/u/thiago)\
**Post date:** [June 13, 2009, 11:50am UTC](https://www.ruby-forum.com/t/noob-question-regarding-array/170184/2 "2009-06-13T11:50:34Z")

</div>

On 13.06.2009 11:36, Thiago wrote:

> ```
> @songs.push(aSong)
> 
> ```
> 
> append(Song.new(‘title4’, ‘artist4’, 4))
> 
> This doesnt make any sense. I can create the Song object just fine,  
> but if i try to add it to the array i get this erro.  
> Can anyone help me please?

You did not show all the code but my guess would be that you did not  
initialize @songs properly in #initialize.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![thiago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ec9cab/32.png) [@thiago](https://www.ruby-forum.com/u/thiago)\
**Post date:** [June 13, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/noob-question-regarding-array/170184/3 "2009-06-13T13:17:20Z")

</div>

yes that was the problem.I just typed initialize incorrectly, so my  
array was not being initialized.I should’ve got a decent ide to avoid  
this stupid mistakes.  
Thanks

---

<div class="post-metadata">

**Author:** ![thiago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ec9cab/32.png) [@thiago](https://www.ruby-forum.com/u/thiago)\
**Post date:** [June 13, 2009, 11:54am UTC](https://www.ruby-forum.com/t/noob-question-regarding-array/170184/4 "2009-06-13T11:54:40Z")

</div>

On Saturday 13 June 2009, Thiago wrote:

> |  
> |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))  
> |  
> |This doesnt make any sense. I can create the Song object just fine,  
> |but if i try to add it to the array i get this erro.  
> |Can anyone help me please?

What do you have in the initialize method of the SongList class? From  
the  
error message you get, it seems that @songs is nil instead of being an  
array.  
To avoid this, you need to set the @songs instance variable to an array  
and  
usually this is done in the initialize method. It should be something  
like  
this:

class SongList

def initialize  
#other initialization code can go here  
@songs = []  
#other initialization code can go here  
end

end

I hope this helps

Stefano
