0

我是 ruby​​ 新手,正在研究一个需要通过 Rspec 的字典项目,但在实现所需的“查找”方法时遇到了一些麻烦。

规格如下:

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

  it "doesn't include a prefix that wasn't added as a word in and of itself" do
    @d.add('fish')
    @d.include?('fi').should be_false
  end

  it "doesn't find a word in empty dictionary" do
    @d.find('fi').should be_empty # {}
  end

  it 'finds nothing if the prefix matches nothing' do
    @d.add('fiend')
    @d.add('great')
    @d.find('nothing').should be_empty
  end

  it "finds an entry" do
    @d.add('fish' => 'aquatic animal')
    @d.find('fish').should == {'fish' => 'aquatic animal'}
  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

  it 'lists keywords alphabetically' do
    @d.add('zebra' => 'African land animal with stripes')
    @d.add('fish' => 'aquatic animal')
    @d.add('apple' => 'fruit')
    @d.keywords.should == %w(apple fish zebra)
  end

  it 'can produce printable output like so: [keyword] "definition"' do
    @d.add('zebra' => 'African land animal with stripes')
    @d.add('fish' => 'aquatic animal')
    @d.add('apple' => 'fruit')
    @d.printable.should == %Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"}
  end
end

到目前为止我写的是

class Dictionary < Hash

    def add(definition)
        definition = {definition => nil} unless definition.kind_of?(Hash)
        merge!(definition)
    end

    alias_method :entries, :dup

    def keywords
        keys.sort 
    end

    def find(definition)
        return{}

    end

end

我真的不知道如何进一步进行。我的 find 方法通过了“在空字典中找不到单词”和“如果前缀不匹配任何内容”部分的测试,尽管我不太确定为什么。我目前被困在

  it "finds an entry" do
    @d.add('fish' => 'aquatic animal')
    @d.find('fish').should == {'fish' => 'aquatic animal'}
  end

我大约 5 天前才开始学习 ruby​​,所以还有很多关键字和语法我不知道如何使用。如果有人能告诉我我做错了什么,并且如果有更好的方法来实现这个方法,那将不胜感激。提前致谢!

4

1 回答 1

0

要传递此规范,您应该简单地返回您在其他方法中添加的内容。假设您实现了该add方法,我认为问题在于您不知道如何[]在上下文而不是变量中调用该方法。你可以这样做:

self[definition]
于 2013-09-06T03:37:30.000 回答