Stefano Crocco
2/29/2008 1:53:00 PM
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