Class: PrettyPrint::SingleLine

Inherits:
Object
  • Object
show all
Defined in:
opal/stdlib/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

Direct Known Subclasses

PP::SingleLine

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.

503
504
505
506
# File 'opal/stdlib/prettyprint.rb', line 503

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.


518
519
520
# File 'opal/stdlib/prettyprint.rb', line 518

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

#first?Boolean

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

Returns:


550
551
552
553
554
# File 'opal/stdlib/prettyprint.rb', line 550

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

#flushObject

Method present for compatibility, but is a noop


546
547
# File 'opal/stdlib/prettyprint.rb', line 546

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.

537
538
539
540
541
542
543
# File 'opal/stdlib/prettyprint.rb', line 537

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.


525
526
527
# File 'opal/stdlib/prettyprint.rb', line 525

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.


511
512
513
# File 'opal/stdlib/prettyprint.rb', line 511

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