Noob question, regarding array

T

Thiago

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

Robert Klemme

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?

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

Kind regards

robert
 
S

Stefano Crocco

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

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
 
T

Thiago Nunes

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: (e-mail address removed)
Subject: Re: Noob question=2C regarding array
To: (e-mail address removed)
=20
|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;Hotmail&utm=
_medium=3DTagline&utm_campaign=3DIE8=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top