Class: PrintSquare::Printer
- Inherits:
-
Object
- Object
- PrintSquare::Printer
- Defined in:
- lib/print_square/printer.rb
Instance Method Summary collapse
-
#initialize(size) ⇒ Printer
constructor
A new instance of Printer.
- #out ⇒ Object
- #set(x, y, n) ⇒ Object
Constructor Details
#initialize(size) ⇒ Printer
Returns a new instance of Printer.
3 4 5 |
# File 'lib/print_square/printer.rb', line 3 def initialize(size) @matrix = (1..size).reduce([]) {|matrix,n| matrix + [(1..size).map { 0 }]} end |
Instance Method Details
#out ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/print_square/printer.rb', line 11 def out column_sizes = @matrix.first.map(&:to_s).map(&:size) out_rows = [] @matrix.each do |row| row.each_with_index do |cell,i| row[i] = cell.to_s.rjust(column_sizes[i]) end out_rows << [row.join(' ')] end puts out_rows.join("\n") end |
#set(x, y, n) ⇒ Object
7 8 9 |
# File 'lib/print_square/printer.rb', line 7 def set(x, y, n) @matrix[y.position][x.position] = n end |