方法"找到"当我把它拉出Class并测试它时似乎工作,但由于某种原因它在Class里面时返回空。我无法弄清楚为什么......
class Dictionary
def entries
@entries ||= {}
end
def add(entry)
if entry.is_a?(String) == true
@entries = {entry => nil}
else
@entries= entry
end
end
def keywords
@entries.keys.sort
end
def include?(word)
entries.keys.include?(word)
end
def find(word)
result = {}
entries.each_pair do |key, value|
if key =~ /#{word}/
result[key] = value
end
end
result
end
end
它停留在规范的这一部分......
it 'finds multiple matches from a prefix and returns the entire entry (keyword + definition)' do
@d.add('fish' => 'aquatic animal')
@d.add('fiend' => 'wicked person')
@d.add('great' => 'remarkable')
@d.find('fi').should == {'fish' => 'aquatic animal', 'fiend' => 'wicked person'}
end
错误说...... 预期:{" fish" => "水生动物"," fiend" => "邪恶的人"} 得到:{}(使用==) 差异:@@ -1,3,1 + 1 @@ - "魔鬼" => "邪恶的人" - "鱼" => "水生动物" #。11_dictionary/dictionary_spec.rb:67:in'阻止(2级)>'
答案 0 :(得分:0)
您的add方法替换整个条目哈希,而不是实际添加条目。修复它,并且find方法应该可以工作。
为了完整起见,我们将如何实施add
:
def add(entry)
if entry.is_a?(Hash)
@entries.merge!(entry)
else
@entries[entry] = nil
end
end