Class: PrettyPrint::Text
- Inherits:
-
Object
- Object
- PrettyPrint::Text
- Defined in:
- lib/prettyprint.rb
Overview
The Text class is the means by which to collect strings from objects.
This class is intended for internal use of the PrettyPrint buffers.
Instance Attribute Summary collapse
-
#width ⇒ Object
readonly
The total width of the objects included in this Text object.
Instance Method Summary collapse
-
#add(obj, width) ⇒ Object
Include
obj
in the objects to be pretty printed, and increment this Text object’s total width bywidth
. -
#initialize ⇒ Text
constructor
Creates a new text object.
-
#output(out, output_width) ⇒ Object
Render the String text of the objects that have been added to this Text object.
Constructor Details
#initialize ⇒ Text
Creates a new text object.
This constructor takes no arguments.
The workflow is to append a PrettyPrint::Text object to the buffer, and being able to call the buffer.last() to reference it.
As there are objects, use PrettyPrint::Text#add to include the objects and the width to utilized by the String version of this object.
310 311 312 313 |
# File 'lib/prettyprint.rb', line 310 def initialize @objs = [] @width = 0 end |
Instance Attribute Details
#width ⇒ Object (readonly)
The total width of the objects included in this Text object.
316 317 318 |
# File 'lib/prettyprint.rb', line 316 def width @width end |
Instance Method Details
#add(obj, width) ⇒ Object
Include obj
in the objects to be pretty printed, and increment this Text object’s total width by width
328 329 330 331 |
# File 'lib/prettyprint.rb', line 328 def add(obj, width) @objs << obj @width += width end |
#output(out, output_width) ⇒ Object
Render the String text of the objects that have been added to this Text object.
Output the text to out
, and increment the width to output_width
321 322 323 324 |
# File 'lib/prettyprint.rb', line 321 def output(out, output_width) @objs.each {|obj| out << obj} output_width + @width end |