Class: Yadriggy::Printer
- Inherits:
-
Object
- Object
- Yadriggy::Printer
- Defined in:
- lib/yadriggy/printer.rb
Overview
A helper class for pretty printing.
Direct Known Subclasses
Instance Method Summary collapse
-
#<<(code) ⇒ Object
Prints the text.
-
#down ⇒ Object
Increase the indentation level.
-
#initialize(indent = 2) ⇒ Printer
constructor
A new instance of Printer.
-
#nl ⇒ Object
Starts a new line.
-
#output ⇒ Object
Returns the output stream.
-
#up ⇒ Object
Decrease the indentation level.
Constructor Details
#initialize(indent = 2) ⇒ Printer
Returns a new instance of Printer.
9 10 11 12 13 14 |
# File 'lib/yadriggy/printer.rb', line 9 def initialize(indent=2) @text = '' @level = 0 @linebreak = false @indent = ' ' * indent end |
Instance Method Details
#<<(code) ⇒ Object
Prints the text. If code
is :nl
, a line break is printed.
46 47 48 49 50 51 52 53 54 |
# File 'lib/yadriggy/printer.rb', line 46 def << (code) add_newline if @linebreak if code == :nl @linebreak = true else @text << code.to_s end self end |
#down ⇒ Object
Increase the indentation level.
25 26 27 28 |
# File 'lib/yadriggy/printer.rb', line 25 def down @level += 1 add_newline end |
#nl ⇒ Object
Starts a new line.
39 40 41 |
# File 'lib/yadriggy/printer.rb', line 39 def nl @linebreak = true end |
#output ⇒ Object
Returns the output stream.
18 19 20 21 |
# File 'lib/yadriggy/printer.rb', line 18 def output() add_newline if @linebreak @text end |
#up ⇒ Object
Decrease the indentation level.
32 33 34 35 |
# File 'lib/yadriggy/printer.rb', line 32 def up @level -= 1 add_newline end |