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 |
# File 'lib/print_square/printer.rb', line 11 def out column_sizes = @matrix.first.map(&:to_s).map(&:size) @matrix.each{|r| r.each_with_index{|c,i| print "#{' ' if i > 0}%#{column_sizes[i]}s" % c}; puts} 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 |