Generic-user-small russel morgan 1 post

I was following the examples in the pickaxe book and I hit one error at run time I was curious about.

When I try the chained append method for the below class like the book I get the following error:

1) Error:
test_delete(TestSongList):
NoMethodError: undefined method `append' for #<array:0xbf580810> ruby1:68:in `test_delete'

Chain Code: list.append(s1).append(s2)

If I do it “Traditionally” like thus:
list.append(s1)
list.append(s2)

It works fine…

I have included the relevant class below, what am I doing wrong? This example appears on Page 48 of the second edition of <u>Programming Ruby</u>

The code I have input:(relevant section)

class SongList def initialize @songs = Array.new end def append(song) @songs.push(song) end def delete_first @songs.shift end def delete_last @songs.pop end def [](index) @songs[index] end def to_s “This song list contains songs” end
end

list.append(s1)
list.append(s2)
list.append(s3)
list.append(s4)
 
Generic-user-small Ben Alpert 1 post

Add a line to your append definition:

class SongList
  def initialize
    @songs = Array.new
  end

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

  def delete_first
    @songs.shift
  end

  def delete_last
    @songs.pop
  end

  def [](index)
    @songs[index]
  end

  def to_s
    "This song list contains songs" 
  end
end

2 posts, 2 voices