From: Thiago Nunes Date: 2009-06-13T20:16:26+09:00 Subject: Re: Noob question, regarding array --_5dc422fc-c447-4cfa-816e-0f3c46b5725f_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable yes that was the problem.I just typed initialize incorrectly=2C so my array= was not being initialized.I should've got a decent ide to avoid this stupi= d mistakes. Thanks > Date: Sat=2C 13 Jun 2009 18:54:10 +0900 > From: stefano.crocco@alice.it > Subject: Re: Noob question=2C regarding array > To: ruby-talk@ruby-lang.org >=20 > On Saturday 13 June 2009=2C Thiago wrote: > > |I'm a noob=2C trying to learn ruby. > > |I'm following along with the book Programming Ruby=2C The Pragmatic > > |Programmer's Guide=2C which comes with ruby documentation. > > | > > |Im on chapter=2C Implementing a SongList Container=2C doing the code > > |samples=2C 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 =3D SongList.new > > |list. > > | append(Song.new('title1'=2C 'artist1'=2C 1)). > > | append(Song.new('title2'=2C 'artist2'=2C 2)). > > | append(Song.new('title3'=2C 'artist3'=2C 3)). > > | append(Song.new('title4'=2C 'artist4'=2C 4)) > > | > > |This doesnt make any sense. I can create the Song object just fine=2C > > |but if i try to add it to the array i get this erro. > > |Can anyone help me please? >=20 > What do you have in the initialize method of the SongList class? From the= =20 > error message you get=2C it seems that @songs is nil instead of being an = array.=20 > To avoid this=2C you need to set the @songs instance variable to an array= and=20 > usually this is done in the initialize method. It should be something lik= e=20 > this: >=20 > class SongList >=20 > def initialize > #other initialization code can go here=20 > @songs =3D [] > #other initialization code can go here=20 > end >=20 > end >=20 > I hope this helps >=20 > Stefano >=20 _________________________________________________________________ Descubra todas as novidades do novo Internet Explorer 8 http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=3DMSN%3BHotmail&utm= _medium=3DTagline&utm_campaign=3DIE8= --_5dc422fc-c447-4cfa-816e-0f3c46b5725f_--