Class: PrettierPrint::Buffer::ArrayBuffer
- Inherits:
-
Object
- Object
- PrettierPrint::Buffer::ArrayBuffer
- Defined in:
- lib/prettier_print.rb
Overview
This is an output buffer that wraps an array output object. It provides a trim! method that trims off trailing whitespace from the last element in the array if it’s an unfrozen string using the same method as the StringBuffer.
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #<<(object) ⇒ Object
-
#initialize(output = []) ⇒ ArrayBuffer
constructor
A new instance of ArrayBuffer.
- #trim! ⇒ Object
Constructor Details
#initialize(output = []) ⇒ ArrayBuffer
Returns a new instance of ArrayBuffer.
306 307 308 |
# File 'lib/prettier_print.rb', line 306 def initialize(output = []) @output = output end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
304 305 306 |
# File 'lib/prettier_print.rb', line 304 def output @output end |
Instance Method Details
#<<(object) ⇒ Object
310 311 312 |
# File 'lib/prettier_print.rb', line 310 def <<(object) @output << object end |
#trim! ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/prettier_print.rb', line 314 def trim! return 0 if output.empty? trimmed = 0 while output.any? && output.last.is_a?(String) && output.last.match?(/\A[\t ]*\z/) trimmed += output.pop.length end if output.any? && output.last.is_a?(String) && !output.last.frozen? length = output.last.length output.last.gsub!(/[\t ]*\z/, "") trimmed += length - output.last.length end trimmed end |