Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > HELP: NameError: uninitialized constant

Reply
Thread Tools

HELP: NameError: uninitialized constant

 
 
Mac Flores
Guest
Posts: n/a
 
      02-29-2008
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/.

 
Reply With Quote
 
 
 
 
Arlen Cuss
Guest
Posts: n/a
 
      02-29-2008
[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

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      02-29-2008
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


 
Reply With Quote
 
Mac Flores
Guest
Posts: n/a
 
      02-29-2008
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/.

 
Reply With Quote
 
Arlen Cuss
Guest
Posts: n/a
 
      02-29-2008
[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

 
Reply With Quote
 
Mac Flores
Guest
Posts: n/a
 
      02-29-2008
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
uninitialized constant ApplicationController (NameError) Daniel Ruby 2 06-22-2006 08:39 AM
ruby - rexml/streamlistner - error: uninitialized constant jsp408@comcast.net Ruby 2 06-15-2006 11:30 AM
ruby-ldap: uninitialized constant LDAP::LDAP_CONTROL_PAGEDRESULTS James Hughes Ruby 4 12-13-2005 11:46 PM
Uninitialized constant objects =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= C++ 5 06-01-2005 11:31 AM
uninitialized constant Magick in RMagick Todd Gardner Ruby 3 06-17-2004 09:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57