Class: Primetable::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/primetable/formatter.rb

Instance Method Summary collapse

Instance Method Details

#format(values) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/primetable/formatter.rb', line 3

def format(values)
  # all cells will have the width of the maximum value
  width = values.map { |row| row.map { |v| v.to_s.length }.max }.max
  
  str = ""
  values.each do |row|
    row.each do |v|
      str << v.to_s.rjust(width) << " | "
    end
    # hack to remove the final space, and add the newline
    str[-1] = "\n"
  end
  str
end