Class: PrettyPrint::Group

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

Overview

A node in the print tree that represents a group of items which the printer should try to fit onto one line. This is the basic command to tell the printer when to break. Groups are usually nested, and the printer will try to fit everything on one line, but if it doesn’t fit it will break the outermost group first and try again. It will continue breaking groups until everything fits (or there are no more groups to break).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(depth, contents: []) ⇒ Group

Returns a new instance of Group.



149
150
151
152
153
# File 'lib/syntax_tree/prettyprint.rb', line 149

def initialize(depth, contents: [])
  @depth = depth
  @contents = contents
  @break = false
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



147
148
149
# File 'lib/syntax_tree/prettyprint.rb', line 147

def contents
  @contents
end

#depthObject (readonly)

Returns the value of attribute depth.



147
148
149
# File 'lib/syntax_tree/prettyprint.rb', line 147

def depth
  @depth
end

Instance Method Details

#breakObject



155
156
157
# File 'lib/syntax_tree/prettyprint.rb', line 155

def break
  @break = true
end

#break?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/syntax_tree/prettyprint.rb', line 159

def break?
  @break
end

#pretty_print(q) ⇒ Object



163
164
165
166
167
# File 'lib/syntax_tree/prettyprint.rb', line 163

def pretty_print(q)
  q.group(2, "group([", "])") do
    q.seplist(contents) { |content| q.pp(content) }
  end
end