我正在关注一本关于 Ruby 的书,我收到一个奇怪的错误,如果有人能为我解决这个问题,我会很高兴。
我正在寻找 Ruby 中的集合(非常基础)
文件:song_list.rb
class Song
attr_accessor :name
attr_accessor :artist
attr_accessor :duration
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
def to_s
"SongIs - #{@name} - #{@artist} - #{@duration}"
end
end
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
end
然后我写了一个测试:test_unit.rb
require_relative 'song_list'
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', 3)
list.append(s1).append(s2).append(s3).append(s4)
assert_equal(s1, list[0])
assert_equal(s2, list[1]);
assert_equal(s3, list[2]);
assert_equal(s4, list[3]);
assert_nil(list[9])
assert_equal(s1, list.delete_first)
assert_equal(s2, list.delete_first)
assert_equal(s4, list.delete_last)
assert_equal(s3, list.delete_last)
assert_nil(list.delete_last)
end
end
现在,当我运行第二个文件时ruby test_unit.rb
,出现错误。错误说我没有append
在 RubyList 类中定义函数。
但我有,不是吗?这是错误。
Run options:
# Running tests:
[1/1] TestSongList#test_delete = 0.00 s
1) Error:
test_delete(TestSongList):
NoMethodError: undefined method `append' for #<Array:0x9f52b30>
test_unit.rb:11:in `test_delete'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:1301:in `run'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/test/unit/testcase.rb:17:in `run'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:919:in `block in _run_suite'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:912:in `map'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:912:in `_run_suite'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/test/unit.rb:657:in `block in _run_suites'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/test/unit.rb:655:in `each'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/test/unit.rb:655:in `_run_suites'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:867:in `_run_anything'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:1060:in `run_tests'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:1047:in `block in _run'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:1046:in `each'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:1046:in `_run'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/unit.rb:1035:in `run'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/test/unit.rb:21:in `run'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/test/unit.rb:774:in `run'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/test/unit.rb:366:in `block (2 levels) in autorun'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/test/unit.rb:27:in `run_once'
/home/ziyan/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/test/unit.rb:365:in `block in autorun'
Finished tests in 0.004259s, 234.8127 tests/s, 0.0000 assertions/s.
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
如果您能向我描述为什么会发生这种情况,我会很高兴。