HELP: NameError: uninitialized constant

Discussion in 'Ruby' started by Mac Flores, Feb 29, 2008.

  1. Mac Flores

    Mac Flores Guest

    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!
    --
    Posted via http://www.ruby-forum.com/.
     
    Mac Flores, Feb 29, 2008
    #1
    1. Advertising

  2. Mac Flores

    Arlen Cuss Guest

    [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
     
    Arlen Cuss, Feb 29, 2008
    #2
    1. Advertising

  3. 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
     
    Stefano Crocco, Feb 29, 2008
    #3
  4. Mac Flores

    Mac Flores Guest

    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 Cuss wrote:
    > Can you verify you get *exactly* the same error when you require
    > 'SongList'
    > at the top of your code?
    >
    > Thanks!
    >
    > Arlen


    --
    Posted via http://www.ruby-forum.com/.
     
    Mac Flores, Feb 29, 2008
    #4
  5. Mac Flores

    Arlen Cuss Guest

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

    Hi,

    On Sat, Mar 1, 2008 at 12:56 AM, Mac Flores <> 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. :)

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

    Arlen
     
    Arlen Cuss, Feb 29, 2008
    #5
  6. Mac Flores

    Mac Flores Guest

    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 Cuss wrote:
    > Hi,
    >
    > On Sat, Mar 1, 2008 at 12:56 AM, Mac Flores <>
    > 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. :)
    >
    > But, I also think you'll find you need to require Song as well,
    > otherwise
    > that won't exist either!
    >
    > Arlen


    --
    Posted via http://www.ruby-forum.com/.
     
    Mac Flores, Feb 29, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel
    Replies:
    2
    Views:
    519
    Daniel
    Jun 22, 2006
  2. Jose Pepe
    Replies:
    1
    Views:
    164
    Jose Pepe
    Jul 16, 2006
  3. Reid Thompson
    Replies:
    0
    Views:
    121
    Reid Thompson
    Oct 21, 2006
  4. Vikash Kumar
    Replies:
    2
    Views:
    124
    Vikash Kumar
    Nov 6, 2006
  5. Jigar Gosar
    Replies:
    8
    Views:
    148
    Khurram Virani
    Apr 3, 2008
Loading...

Share This Page