2014-01-29 37 views
0

爲什麼第一次測試返回173,而不是81,第二個20,而不是9?我如何從@cells.each_slice(9).map { |v| v } - count方法返回數組的數量?Sudoku解決方案的開始:2個rspec失敗

class Grid 

    attr_reader :cells 

    def initialize(puzzle) 
    @cells = puzzle.chars.map { |x| (x.to_i) } 
    end 

    def rows 
    @rows = @cells.each_slice(9).map { |v| v } 
    end 
end 

規範

require 'Grid' 

    describe Grid do 

    context "initialization" do 
     let(:puzzle) { '01500300200010090627 
         00684304900020175010 
         40380003905000900081 
         04086007002503720460 
         0'} 
     let(:grid) { Grid.new(puzzle) } 

     it 'should have 81 cells' do 
     expect(grid.cells.length).to eq(81) 
     end 

     it 'should have an unsolved first cell' do 
     expect(grid.cells.first).to eq(0) 
     end 

     it 'should have 9 rows' do 
     expect(grid.rows).to eq(9) 
     end 
    end 
    end 

rspec的報告

Grid 
    initialization 
    should have 81 cells (FAILED - 1) 
    should have an unsolved first cell 
    should have 9 rows (FAILED - 2) 

Failures: 

    1) Grid initialization should have 81 cells 
    Failure/Error: expect(grid.cells.length).to eq(81) 

     expected: 81 
      got: 173 

     (compared using ==) 
    # ./spec/grid_spec.rb:14:in `block (3 levels) in <top (required)>' 

    2) Grid initialization should have 9 rows 
    Failure/Error: expect(grid.rows).to eq(9) 

     expected: 9 
      got: 20 

     (compared using ==) 
    # ./spec/grid_spec.rb:22:in `block (3 levels) in <top (required)>' 

Finished in 0.00165 seconds 
3 examples, 2 failures 

回答

1

我複製並粘貼數字串入IRB,發現其中173是來自:

irb(main):001:0> a = '01500300200010090627 
irb(main):002:0'      00684304900020175010 
irb(main):003:0'      40380003905000900081 
irb(main):004:0'      04086007002503720460 
irb(main):005:0'      0' 
=> "01500300200010090627\n      00684304900020175010\n      40380003905000900081\n      04086007002503720460\n      0" 
irb(main):006:0> a.length 
=> 173 

由於你可以看到它是inclu換行符和空格。你可能希望這樣的:

let(:puzzle) { '01500300200010090627' + 
       '00684304900020175010' + 
       '40380003905000900081' + 
       '04086007002503720460' + 
       '0' } 

關於@rows我猜你想

@rows = @cells.each_slice(9).to_a 

,但在您的測試,你應該有

expect(grid.rows.size).to eq(9) 
+0

感謝。任何想法如何我可以返回'@rows = @ cells.each_slice(9).map {| v | v}'? – HandDisco

+0

@Abraham回答更新以解決此問題。 – Matt

+0

當然! 'to_a' then'.count' return 9.我過於複雜了。謝謝。 – HandDisco

1

不要更改線路

@cells = puzzle.chars.map { |x| (x.to_i) } 

作爲

@cells = puzzle.gsub(/[^0-9]/,'').chars.map { |x| (x.to_i) } 

全碼:

class Grid 

    attr_reader :cells 

    def initialize(puzzle) 
    @cells = puzzle.gsub(/[^0-9]/,'').chars.map { |x| (x.to_i) } 
    end 

    def rows 
    @rows = @cells.each_slice(9).map { |v| v } 
    end 
end 

s = '01500300200010090627 
    00684304900020175010 
    40380003905000900081 
    04086007002503720460 
         0' 
grid = Grid.new(s) 
grid.cells.length # => 81 
grid.rows.size # => 9 
+0

這是做什麼'gsub(/ [^ 0-9] /,'')'?試了一下,rspec返回一個數組中的9個數組。 – HandDisco

+1

今天早上再試一次,它確實有效。謝謝。 – HandDisco

+1

我意識到雖然有趣的'gsub(/ [^ 0-9] /,'')'是不必要的。 – HandDisco

相關問題