我有以下块,它允许我添加带有关键字和定义的整个条目:
class Dictionary
def initialize
@d = {}
end
def entries
@d
end
def add(entry)
(entry).each {|k,v=nil|
@d[k] = v
}
end
def keywords
@d.keys
end
end
我希望能够添加没有定义的关键字(添加键,但没有值)。我尝试在上面的块中设置 v=nil ,但它仍然失败。这是我在针对 Rspec 运行时遇到的错误:
Dictionary
is empty when created
can add whole entries with keyword and definition
add keywords (without definition) (FAILED - 1)
Failures:
1) Dictionary add keywords (without definition)
Failure/Error: @d.add('fish')
NoMethodError:
undefined method `each' for "fish":String
# ./11_dictionary/dictionary.rb:13:in `add'
# ./11_dictionary/dictionary_spec.rb:27:in `block (2 levels) in <top (required)>'
最后,这是它需要通过的 rspec 测试:
it 'add keywords (without definition)' do
@d.add('fish')
@d.entries.should == {'fish' => nil}
@d.keywords.should == ['fish']
end