Class: Output
- Inherits:
-
Object
- Object
- Output
- Defined in:
- lib/output.rb
Instance Attribute Summary collapse
-
#indent_character ⇒ Object
For indentation.
-
#indent_step ⇒ Object
For indentation.
-
#last_indent ⇒ Object
Returns the value of attribute last_indent.
-
#tab ⇒ Object
Returns the value of attribute tab.
-
#tab_replaces_count ⇒ Object
Returns the value of attribute tab_replaces_count.
Instance Method Summary collapse
-
#initialize ⇒ Output
constructor
A new instance of Output.
- #join(blocks, separator = "\n") ⇒ Object
Constructor Details
#initialize ⇒ Output
Returns a new instance of Output.
15 16 17 18 19 20 21 |
# File 'lib/output.rb', line 15 def initialize @indent_character = ' ' @indent_step = 4 @tab = "\t" @tab_replaces_count = 0 @last_indent = 0 end |
Instance Attribute Details
#indent_character ⇒ Object
For indentation.
11 12 13 |
# File 'lib/output.rb', line 11 def indent_character @indent_character end |
#indent_step ⇒ Object
For indentation.
11 12 13 |
# File 'lib/output.rb', line 11 def indent_step @indent_step end |
#last_indent ⇒ Object
Returns the value of attribute last_indent.
13 14 15 |
# File 'lib/output.rb', line 13 def last_indent @last_indent end |
#tab ⇒ Object
Returns the value of attribute tab.
12 13 14 |
# File 'lib/output.rb', line 12 def tab @tab end |
#tab_replaces_count ⇒ Object
Returns the value of attribute tab_replaces_count.
12 13 14 |
# File 'lib/output.rb', line 12 def tab_replaces_count @tab_replaces_count end |
Instance Method Details
#join(blocks, separator = "\n") ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/output.rb', line 23 def join(blocks, separator = "\n") indented = [] blocks.flatten! indent = 0 blocks.each do |block| if block.nil? indent = 0 elsif block.is_a?(Integer) indent += block elsif block.is_a?(TrueClass) indent += @indent_step elsif block.is_a?(FalseClass) indent -= @indent_step else block = block.to_s unless block.is_a?(String) if indent.zero? indented.push(block) next end if 0 < @tab_replaces_count tabs = @tab * (indent / @tab_replaces_count) chars = @indent_character * (indent % @tab_replaces_count) else tabs = '' chars = @indent_character * indent end lines = block.lines(chomp: true) lines.each do |line| indented.push("#{tabs}#{chars}#{line}") end end end @last_indent = indent indented.join(separator) end |