[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Noob question, regarding array

Thiago

6/13/2009 9:36:00 AM

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?

3 Answers

Robert Klemme

6/13/2009 9:49:00 AM

0

On 13.06.2009 11:36, Thiago wrote:
> 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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Stefano Crocco

6/13/2009 9:54:00 AM

0

On Saturday 13 June 2009, Thiago wrote:
> |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

Thiago Nunes

6/13/2009 11:16:00 AM

0


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%3BHotmai...
_medium=3DTagline&utm_campaign=3DIE8=