0

私は TestFirst.org の Ruby チュートリアルに取り組んでいます。私は現在「辞書」のレッスンを受けています。キーワードの例にこだわっています。Ruby ドキュメントと RSpec ファイルを次に示します。

私が混乱している理由は、例が正しいかどうかわからないからです。RSpecファイルに従うと、「特定のキーワードが存在するかどうかを確認できる」の前の例では、addメソッドを使用して「魚」を辞書に追加しています。次に、エラーが表示される例では、include? メソッド (実装する必要があります) は false を返す必要があります。

false を返す必要がありますか? 上記の例に「魚」を追加しませんでしたか? または、何か不足していますか?

これは私のRubyプログラムです:

class Dictionary
    def initialize
        @entries = {}
    end

    def entries
        @entries
    end

    def keywords
        @entries.keys
    end

    def add(pairings)
        if pairings.is_a?(String)
            @entries[pairings] = nil 
        else
            pairings.each do |word, definition|
                @entries[word] = definition
            end
        end
    end

    def include?(keyword)
        @entries.keys.include?(keyword)
    end
end

以下は 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
4

1 に答える 1