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.
    


505
506
507
508
# File 'lib/prettyprint.rb', line 505

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.



520
521
522
# File 'lib/prettyprint.rb', line 520

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

#first?Boolean

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

Returns:

  • (Boolean)


552
553
554
555
556
# File 'lib/prettyprint.rb', line 552

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

#flushObject

Method present for compatibility, but is a noop



548
549
# File 'lib/prettyprint.rb', line 548

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.



539
540
541
542
543
544
545
# File 'lib/prettyprint.rb', line 539

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.



527
528
529
# File 'lib/prettyprint.rb', line 527

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.



513
514
515
# File 'lib/prettyprint.rb', line 513

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