2013-03-07 103 views
0

所以我正在解决RSPEC问题,并且我在这个网站上获得的所有帮助都很棒。尽管如此,我也遇到了一些麻烦。从红宝石转换为红色的哈希

的想法是在Ruby中创建一个Dictionary对象和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 
    attr_accessor :keywords, :entries 

    def initialize 
    @entries = {} 
    end 

    def add(defs) 
    defs.each do |word, definition| 
     @entries[word] = definition 
    end  
    end 

    def keywords 
    @entries.keys.sort 
    end 

    def include?(key) 
    @entries.key?(key) 
    end 

    def find(query) 
    @entries.select { |word, definition| word.scan(query).join == query} 
    end  
end 

和我得到的该测试的错误是:

1) Dictionary finds an entry 
    Failure/Error: @d.find('fish').should == {'fish' => 'aquatic animal'} 
     expected: {"fish"=>"aquatic animal"} 
      got: [["fish", "aquatic animal"]] (using ==) 
     Diff: 
     @@ -1,2 +1,2 @@ 
     -"fish" => "aquatic animal" 
     +[["fish", "aquatic animal"]] 

因此,它看起来就像是从查找方法是一个数组,而不是一个Hash输出的问题。解决这个问题最好的方法是什么?先谢谢你!

+1

下一次,考虑让你的问题最小化;这里不需要完整的实现和规范。 – 2013-03-07 16:14:07

回答

3

您必须使用相当旧版本的Ruby。

在Ruby 1.8.7:

{}.select{} # => [] 

使用最新的Ruby:

{}.select{} # => {} 

,你可以:

  • 升级的Ruby 1.9.2+
  • 使用reject取而代之的是逆转你的状况
  • 或使用我的backports gem和require 'backports/force/hash/select'在Ruby 1.8.x中获得相同的行为。
0

尝试采取的选择,做返回的结果:

Hash[result] 

或者更原始地,只是做一个哈希!

{result.first.first => result.first.last}