Question

Why is the first test returning 173, not 81 and the second 20, not 9? How can i return the number of arrays from @cells.each_slice(9).map { |v| v } - count method?

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

spec

 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 report

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
Était-ce utile?

La solution

I copied and pasted the numeric string into irb and found where the 173 is coming from:

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

As you can see it is including the newlines and spaces. You probably want this:

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

Regarding @rows I am guessing you want

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

but in your test you should have

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

Autres conseils

Do change the line

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

as

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

Full code :

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top