현재 루비에서 스도쿠 해결사를 작성 중입니다. 열거자를 사용하여 스도쿠 게임에서 3x3 상자를 참조하는 9x9 격자를 기반으로 9 개의 배열을 만드는 메서드를 만들었습니다. Rspec에서 이것을 테스트하는 동안 한 테스트에서만 작동한다는 것을 알았습니다. 같은 방법으로 두 번째 테스트를 만들면 항상 실패합니다. 내가 주변에서 혼란 스러울 때 그들은 개별적으로 일하지만 두 번째 테스트에서 그 방법이 호출 될 때가 아닙니다. 이상적으로 내 메서드를 내 initialize 메서드에 추가하고 싶지만이 메서드는 모든 테스트를 하나씩 실패합니다. 내가 얻는 오류는 'StopIteration : 반복이 끝났습니다'입니다. 내가 이해할 수는 있지만 왜 각 테스트에서 다시 시작하지 못합니까? 어떤 아이디어?루비에서 열거자를 사용하여 메소드를 테스트하는 Rspec. 메서드는 이후에 실패한 경우에만 실행됩니다.
class Grid
BoxOfIndex = [
0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,2,2,2,
3,3,3,4,4,4,5,5,5,3,3,3,4,4,4,5,5,5,3,3,3,4,4,4,5,5,5,
6,6,6,7,7,7,8,8,8,6,6,6,7,7,7,8,8,8,6,6,6,7,7,7,8,8,8
].each
attr_accessor :cells, :rows, :columns, :boxes
def initialize(puzzle)
@cells = puzzle.split('').map {|v| Cell.new(v) }
create_boxes
end
def create_rows
@rows = cells.each_slice(9).to_a
end
def create_columns
@columns = create_rows.transpose
end
def create_boxes
@boxes = []
9.times { @boxes << Array.new}
@cells.each{|cell| @boxes[BoxOfIndex.next].concat([cell])}
end
....................Tests below(second test fails)
it "should be able to create boxes with a cell value" do
grid.create_boxes
expect(grid.boxes[0][2].value).to eq(5)
end
it "should be able to find neighbours of a cell" do
grid.create_boxes
end
.each가 열거자를 이용하여 마련된다. –