2013-03-07 11 views
0

나는 RSPEC 문제를 해결하기 위해 노력하고 있으며이 사이트에서 얻은 모든 도움은 훌륭했습니다. 나는 그것으로 조금 걸림돌을 때렸다.배열에서 Ruby의 해시로 변환

아이디어는 사전 루비 객체와에가 "이 항목을 찾아"까지 내가 모든 것을 가지고

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 

같은 RSpec에 코드의 모양을 만드는 것입니다 내가 곤경에 실행 한 곳입니다. 내 코드는 지금까지 이렇게 보입니다. 그리고이 사이트의 모든 사람들에게 저에게 코드 작성을 도와 주신 모든 분들께 감사드립니다.

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"]] 

그래서이 보인다. 그 문제를 해결하는 가장 좋은 방법은 무엇입니까? 미리 감사드립니다.

+1

다음 번에 질문을 최소화하십시오. 완전한 구현과 사양은 필요 없습니다. –

답변

3

상당히 오래된 버전의 Ruby를 사용해야합니다. 루비 1.8.7에서

:

{}.select{} # => [] 

최신 루비 :

{}.select{} # => {} 

당신은 할 수 :

  • 업그레이드 루비는 1.9.2+하는
  • 사용 reject 대신 귀하의 조건을 반전하십시오.
  • 또는 내 backports gem과 require 'backports/force/hash/select'을 사용하여 Ruby 1.8.x에서 동일한 동작을 얻으십시오.
0

선택하고 수행하여 반환 된 결과를 측정 해보십시오 :

Hash[result] 

또는 더 원시적 단지 해시를 만들!

{result.first.first => result.first.last} 
관련 문제