HELP: NameError: uninitialized constant

M

Mac Flores

OK. I'm learning Ruby by going through the examples in Programming Ruby
by Dave Thomas. Somehow I'm stuck and I cannot progress further. I
have 3 files all in the same folder -- Song.rb, SongList.rb and
TestSongList.rb. Code is as follows:

## Song.rb
class Song
attr_reader :name, :artist, :duration
attr_writer :duration
@@plays=0
def initialize(name,artist,duration)
@name=name
@artist=artist
@duration=duration
@plays=0
end
end

## SongList.rb
def SongList
def initialize
@songs = Array.new
end

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

## TestSongList.rb
require 'test/unit'
class TestSongList < Test::Unit::TestCase
def test_delete
list = SongList.new
s1 = Song.new('title1','artist1',1)
s2 = Song.new('title2','artist2',2)
s3 = Song.new('title3','artist3',3)
s4 = Song.new('title4','artist4',4)
list.append(s1).append(s2).append(s3).append(s4)

assert_equal(s1, list[0])
end
end

So I run the test and I get this:
[ookman@foorubyfiles]$ ruby ./TestSongList.rb
Loaded suite ./TestSongList
Started
E
Finished in 0.000414 seconds.

1) Error:
test_delete(TestSongList):
NameError: uninitialized constant TestSongList::SongList
./TestSongList.rb:4:in `test_delete'

1 tests, 0 assertions, 0 failures, 1 errors

Can anybody tell me why it's giving me the above error on this line?
list = SongList.new

I've tried to do:
require 'SongList' at the top of TestSongList.rb but I get the same
error. I've Googled this problem but I cannot find a resolution. I also
tried to run the TestSongList code one line at a time in irb and it
gives me the same error when it reaches line 4.

Ruby version is:
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]

I appreciate any insight .... TIA!
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

Can you verify you get *exactly* the same error when you require 'SongList'
at the top of your code?

Thanks!

Arlen
 
S

Stefano Crocco

Alle Friday 29 February 2008, Mac Flores ha scritto:
OK. I'm learning Ruby by going through the examples in Programming Ruby
by Dave Thomas. Somehow I'm stuck and I cannot progress further. I
have 3 files all in the same folder -- Song.rb, SongList.rb and
TestSongList.rb. Code is as follows:

## Song.rb
class Song
attr_reader :name, :artist, :duration
attr_writer :duration
@@plays=0
def initialize(name,artist,duration)
@name=name
@artist=artist
@duration=duration
@plays=0
end
end

## SongList.rb
def SongList
def initialize
@songs = Array.new
end

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

## TestSongList.rb
require 'test/unit'
class TestSongList < Test::Unit::TestCase
def test_delete
list = SongList.new
s1 = Song.new('title1','artist1',1)
s2 = Song.new('title2','artist2',2)
s3 = Song.new('title3','artist3',3)
s4 = Song.new('title4','artist4',4)
list.append(s1).append(s2).append(s3).append(s4)

assert_equal(s1, list[0])
end
end

So I run the test and I get this:
[ookman@foorubyfiles]$ ruby ./TestSongList.rb
Loaded suite ./TestSongList
Started
E
Finished in 0.000414 seconds.

1) Error:
test_delete(TestSongList):
NameError: uninitialized constant TestSongList::SongList
./TestSongList.rb:4:in `test_delete'

1 tests, 0 assertions, 0 failures, 1 errors

Can anybody tell me why it's giving me the above error on this line?
list = SongList.new

I've tried to do:
require 'SongList' at the top of TestSongList.rb but I get the same
error. I've Googled this problem but I cannot find a resolution. I also
tried to run the TestSongList code one line at a time in irb and it
gives me the same error when it reaches line 4.

Ruby version is:
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]

I appreciate any insight .... TIA!

There are two problems: you need to add

require 'SongList'

in TestSongList.rb. The second is you try to create the class SongList (in
SongList.rb) using

def SongList

this creates a method called SongList, not a class. To create a class, you
need

class SongList

(as you correctly used to create class Song).

I hope this helps

Stefano
 
M

Mac Flores

Arlen,

Thanks for your response. I get the same error when I do the require:

require 'test/unit'
require 'SongList'
class TestSongList < Test::Unit::TestCase
def test_delete
list = SongList.new
s1 = Song.new('title1','artist1',1)
s2 = Song.new('title2','artist2',2)
s3 = Song.new('title3','artist3',3)
s4 = Song.new('title4','artist4',4)
list.append(s1).append(s2).append(s3).append(s4)

assert_equal(s1, list[0])
end
end

[ookman@foo rubyfiles]$ ruby ./TestSongList.rb
Loaded suite ./TestSongList
Started
E
Finished in 0.000413 seconds.

1) Error:
test_delete(TestSongList):
NameError: uninitialized constant TestSongList::SongList
./TestSongList.rb:7:in `test_delete'

1 tests, 0 assertions, 0 failures, 1 errors
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

Hi,

Arlen,

Thanks for your response. I get the same error when I do the require:

Ah, see Stefano's answer. Use `class', not `def'. An accidental typo. :)

But, I also think you'll find you need to require Song as well, otherwise
that won't exist either!

Arlen
 
M

Mac Flores

Aarrgh! It's a typo!

Thanks, Arlen and Stefano! I've been looking at that code for so many
hours that I missed the 'class' declaration for SongList and was using
'def' instead like you observed. I changed that among other things. I
also added require 'Song' in the SongList class and require 'SongList'
in the TestSongList test class. The test runs now passes.

I appreciate your help and the extra set of eyes.
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top