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.



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

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

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



145
146
147
# File 'lib/syntax_tree/prettyprint.rb', line 145

def contents
  @contents
end

#depthObject (readonly)

Returns the value of attribute depth.



145
146
147
# File 'lib/syntax_tree/prettyprint.rb', line 145

def depth
  @depth
end

Instance Method Details

#breakObject



153
154
155
# File 'lib/syntax_tree/prettyprint.rb', line 153

def break
  @break = true
end

#break?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/syntax_tree/prettyprint.rb', line 157

def break?
  @break
end

#pretty_print(q) ⇒ Object



161
162
163
164
165
# File 'lib/syntax_tree/prettyprint.rb', line 161

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