Class: DiverDown::Web::IndentedStringIo
- Inherits:
-
Object
- Object
- DiverDown::Web::IndentedStringIo
- Extended by:
- Forwardable
- Defined in:
- lib/diver_down/web/indented_string_io.rb
Instance Attribute Summary collapse
-
#indent ⇒ Object
Returns the value of attribute indent.
Instance Method Summary collapse
-
#indented ⇒ Object
increase the indent level for the block.
-
#initialize(tab: ' ') ⇒ IndentedStringIo
constructor
A new instance of IndentedStringIo.
- #puts(*contents, indent: true) ⇒ void
- #write(*contents, indent: true) ⇒ void
Constructor Details
#initialize(tab: ' ') ⇒ IndentedStringIo
Returns a new instance of IndentedStringIo.
16 17 18 19 20 |
# File 'lib/diver_down/web/indented_string_io.rb', line 16 def initialize(tab: ' ') @io = StringIO.new @indent = 0 @tab = tab end |
Instance Attribute Details
#indent ⇒ Object
Returns the value of attribute indent.
13 14 15 |
# File 'lib/diver_down/web/indented_string_io.rb', line 13 def indent @indent end |
Instance Method Details
#indented ⇒ Object
increase the indent level for the block
51 52 53 54 55 56 |
# File 'lib/diver_down/web/indented_string_io.rb', line 51 def indented @indent += 1 yield ensure @indent -= 1 end |
#puts(*contents, indent: true) ⇒ void
This method returns an undefined value.
45 46 47 48 |
# File 'lib/diver_down/web/indented_string_io.rb', line 45 def puts(*contents, indent: true) write("#{contents.join("\n")}\n", indent:) nil end |
#write(*contents, indent: true) ⇒ void
This method returns an undefined value.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/diver_down/web/indented_string_io.rb', line 25 def write(*contents, indent: true) indent_string = if indent @tab * @indent else '' end string = contents.join lines = string.lines lines.each do |line| if line == "\n" @io.write "\n" else @io.write "#{indent_string}#{line}" end end end |