HELP: NameError: uninitialized constant

OK. I’m learning Ruby by going through the examples in Programming Ruby
by Dave T… 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!

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

Thanks!

Arlen

Alle Friday 29 February 2008, Mac F. ha scritto:

def initialize(name,artist,duration)
    @songs = Array.new

class TestSongList < Test::Unit::TestCase
end
NameError: uninitialized constant TestSongList::SongList
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

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

Arlen C. wrote:

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

Thanks!

Arlen

Hi,

On Sat, Mar 1, 2008 at 12:56 AM, Mac F. [email protected]
wrote:

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. :slight_smile:

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

Arlen

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.

Arlen C. wrote:

Hi,

On Sat, Mar 1, 2008 at 12:56 AM, Mac F. [email protected]
wrote:

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. :slight_smile:

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

Arlen