所以我试图在Ruby中创建一个字典对象,并让它通过一堆RSPEC测试作为项目的一部分。到目前为止它一直很好,但是我被困在一个特定的测试中。这里的RSPEC,直到该测试:Ruby中的字典对象
require 'dictionary'
describe Dictionary do
before do
@d = Dictionary.new
end
it 'is empty when created' do
@d.entries.should == {}
end
it 'can add whole entries with keyword and definition' do
@d.add('fish' => 'aquatic animal')
@d.entries.should == {'fish' => 'aquatic animal'}
@d.keywords.should == ['fish']
end
it 'add keywords (without definition)' do
@d.add('fish')
@d.entries.should == {'fish' => nil}
@d.keywords.should == ['fish']
end
it 'can check whether a given keyword exists' do
@d.include?('fish').should be_false
end
it "doesn't cheat when checking whether a given keyword exists" do
@d.include?('fish').should be_false # if the method is empty, this test passes with nil returned
@d.add('fish')
@d.include?('fish').should be_true # confirms that it actually checks
@d.include?('bird').should be_false # confirms not always returning true after add
end
end
一切经过到目前为止除了最后测试“检查一个给定的关键字是否存在时,不会欺骗”。我试图围绕如何让这个通过,但迄今没有成功。任何帮助将不胜感激。这是我到目前为止:
class Dictionary
attr_accessor :keywords, :entries
def initialize
@entries = {}
end
def add(defs)
defs.each do |word, definition|
@entries[word] = definition
end
end
def keywords
input = []
@entries.each do |key, value|
input << key
end
input.sort
end
def include?(key)
self.keywords.include?(keywords.to_s)
end
end
在此先感谢!
“在检查给定关键字是否存在时不作弊”中的哪一个失败? – sawa 2013-03-05 16:52:21
原谅我,如果这是粗鲁的,但我可以问这个对象的重点是什么?我没有看到任何不是由哈希提供的功能.. – 2013-03-05 16:52:26
你忽略提及它如何失败。 – 2013-03-05 16:54:00