Class: TablePrint::FixedWidthFormatter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width) ⇒ FixedWidthFormatter

Returns a new instance of FixedWidthFormatter.



23
24
25
# File 'lib/table_print/formatter.rb', line 23

def initialize(width)
  self.width = width
end

Instance Attribute Details

#widthObject

Returns the value of attribute width.



21
22
23
# File 'lib/table_print/formatter.rb', line 21

def width
  @width
end

Instance Method Details

#format(value) ⇒ Object



27
28
29
30
# File 'lib/table_print/formatter.rb', line 27

def format(value)
  padding = width - length(value.to_s)
  truncate(value) + (padding < 0 ? '' : " " * padding)
end