class ILoveSudoku::PuzzleFormatter

Public Class Methods

new(matrix) click to toggle source
# File lib/puzzle_formatter.rb, line 4
def initialize(matrix)
  @matrix = matrix
end

Public Instance Methods

to_s() click to toggle source
# File lib/puzzle_formatter.rb, line 8
def to_s
  rows = matrix_with_rows_formatted
  [ rows[0..2].join("\n"),
    rows[3..5].join("\n"),
    rows[6..8].join("\n") ].join(divider)
end

Private Instance Methods

divider() click to toggle source
# File lib/puzzle_formatter.rb, line 37
def divider
  "\n---+---+---\n"
end
format_row(row) click to toggle source
# File lib/puzzle_formatter.rb, line 31
def format_row(row)
  [ row[0..2].join(""),
    row[3..5].join(""),
    row[6..8].join("") ].join("|")
end
matrix_with_nils_replaced() click to toggle source
# File lib/puzzle_formatter.rb, line 23
def matrix_with_nils_replaced
  @matrix.map do |row|
    row.map do |cell|
      cell.nil? ? "." : cell
    end
  end
end
matrix_with_rows_formatted() click to toggle source
# File lib/puzzle_formatter.rb, line 17
def matrix_with_rows_formatted
  matrix_with_nils_replaced.each_with_object([]) do |row, output|
    output << format_row(row)
  end
end