Class: Aligntext::Formatter
- Inherits:
-
Object
- Object
- Aligntext::Formatter
- Defined in:
- lib/aligntext/formatter.rb
Instance Method Summary collapse
- #align(text) ⇒ Object
-
#initialize(options = {}) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(options = {}) ⇒ Formatter
Returns a new instance of Formatter.
3 4 5 6 |
# File 'lib/aligntext/formatter.rb', line 3 def initialize(={}) @options = @separator = [:separator] || " " end |
Instance Method Details
#align(text) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/aligntext/formatter.rb', line 8 def align(text) aligned_text = "" max_index = 0 text.each_line do |line| index = line.index(@separator) next unless index if index > max_index max_index = index end end text.each_line do |line| index = line.index(@separator) unless index aligned_text << line next end if index < max_index aligned_text << line.insert(index, " " * (max_index - index)) else aligned_text << line end end aligned_text end |