Class: PrettyPrint::SingleLine

Inherits:
Object
  • Object
show all
Defined in:
lib/prettyprint.rb

Overview

PrettyPrint::SingleLine is used by PrettyPrint.singleline_format

It is passed to be similar to a PrettyPrint object itself, by responding to:

  • #text

  • #breakable

  • #nest

  • #group

  • #flush

  • #first?

but instead, the output has no line breaks

Instance Method Summary collapse

Constructor Details

#initialize(output, maxwidth = nil, newline = nil) ⇒ SingleLine

Create a PrettyPrint::SingleLine object

Arguments:

  • output - String (or similar) to store rendered text. Needs to respond to ‘<<’

  • maxwidth - Argument position expected to be here for compatibility.

    This argument is a noop.
    
  • newline - Argument position expected to be here for compatibility.

    This argument is a noop.
    


523
524
525
526
# File 'lib/prettyprint.rb', line 523

def initialize(output, maxwidth=nil, newline=nil)
  @output = output
  @first = [true]
end

Instance Method Details

#breakable(sep = ' ', width = nil) ⇒ Object

Appends sep to the text to be output. By default sep is ‘ ’

width argument is here for compatibility. It is a noop argument.



538
539
540
# File 'lib/prettyprint.rb', line 538

def breakable(sep=' ', width=nil)
  @output << sep
end

#first?Boolean

This is used as a predicate, and ought to be called first.

Returns:

  • (Boolean)


570
571
572
573
574
# File 'lib/prettyprint.rb', line 570

def first?
  result = @first[-1]
  @first[-1] = false
  result
end

#flushObject

Method present for compatibility, but is a noop



566
567
# File 'lib/prettyprint.rb', line 566

def flush # :nodoc:
end

#group(indent = nil, open_obj = '', close_obj = '', open_width = nil, close_width = nil) ⇒ Object

Opens a block for grouping objects to be pretty printed.

Arguments:

  • indent - noop argument. Present for compatibility.

  • open_obj - text appended before the &blok. Default is ”

  • close_obj - text appended after the &blok. Default is ”

  • open_width - noop argument. Present for compatibility.

  • close_width - noop argument. Present for compatibility.



557
558
559
560
561
562
563
# File 'lib/prettyprint.rb', line 557

def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
  @first.push true
  @output << open_obj
  yield
  @output << close_obj
  @first.pop
end

#nest(indent) ⇒ Object

Takes indent arg, but does nothing with it.

Yields to a block.



545
546
547
# File 'lib/prettyprint.rb', line 545

def nest(indent) # :nodoc:
  yield
end

#text(obj, width = nil) ⇒ Object

Add obj to the text to be output.

width argument is here for compatibility. It is a noop argument.



531
532
533
# File 'lib/prettyprint.rb', line 531

def text(obj, width=nil)
  @output << obj
end