Module: Wood::IndentedPrinter
- Defined in:
- lib/wood/indented_printer.rb
Overview
Mixin used for indented pretty-printing of code. Expects ‘#io` and `#indentation` methods to work.
Instance Method Summary collapse
-
#indent ⇒ Object
Increases the current indentation by ‘indentation` spaces.
-
#newline ⇒ Object
Inserts a line break into ‘io` together with correct reindentation on the next line.
-
#print(*args) ⇒ Object
Prints any arguments passed to ‘io`.
-
#println(str) ⇒ Object
Prints a String to ‘io` followed by a line break.
-
#unindent ⇒ Object
Decreases the current indentation by ‘indentation` spaces.
-
#with_indentation ⇒ Object
Yields to a given block while indenting the output generated within the block.
Instance Method Details
#indent ⇒ Object
Increases the current indentation by ‘indentation` spaces.
37 38 39 40 |
# File 'lib/wood/indented_printer.rb', line 37 def indent @__indent__ ||= 0 @__indent__ += indentation end |
#newline ⇒ Object
Inserts a line break into ‘io` together with correct reindentation on the next line.
31 32 33 34 |
# File 'lib/wood/indented_printer.rb', line 31 def newline io << "\n" io << (" " * @__indent__.to_i) end |
#print(*args) ⇒ Object
Prints any arguments passed to ‘io`.
17 18 19 20 21 |
# File 'lib/wood/indented_printer.rb', line 17 def print(*args) args.each do |a| io << a end end |
#println(str) ⇒ Object
Prints a String to ‘io` followed by a line break.
25 26 27 |
# File 'lib/wood/indented_printer.rb', line 25 def println(str) print str, "\n" end |
#unindent ⇒ Object
Decreases the current indentation by ‘indentation` spaces.
43 44 45 46 |
# File 'lib/wood/indented_printer.rb', line 43 def unindent @__indent__ ||= 0 @__indent__ -= indentation end |
#with_indentation ⇒ Object
Yields to a given block while indenting the output generated within the block.
7 8 9 10 11 12 13 |
# File 'lib/wood/indented_printer.rb', line 7 def with_indentation indent newline yield unindent newline end |