Class: SyntaxTree::MAssign

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

Overview

MAssign is a parent node of any kind of multiple assignment. This includes splitting out variables on the left like:

first, second, third = value

as well as splitting out variables on the right, as in:

value = first, second, third

Both sides support splats, as well as variables following them. There’s also destructuring behavior that you can achieve with the following:

first, = value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(target:, value:, location:, comments: []) ⇒ MAssign

Returns a new instance of MAssign.



6254
6255
6256
6257
6258
6259
# File 'lib/syntax_tree/node.rb', line 6254

def initialize(target:, value:, location:, comments: [])
  @target = target
  @value = value
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6252
6253
6254
# File 'lib/syntax_tree/node.rb', line 6252

def comments
  @comments
end

#targetObject (readonly)

MLHS | MLHSParen

the target of the multiple assignment



6246
6247
6248
# File 'lib/syntax_tree/node.rb', line 6246

def target
  @target
end

#valueObject (readonly)

untyped

the value being assigned



6249
6250
6251
# File 'lib/syntax_tree/node.rb', line 6249

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



6261
6262
6263
# File 'lib/syntax_tree/node.rb', line 6261

def accept(visitor)
  visitor.visit_massign(self)
end

#child_nodesObject Also known as: deconstruct



6265
6266
6267
# File 'lib/syntax_tree/node.rb', line 6265

def child_nodes
  [target, value]
end

#deconstruct_keys(_keys) ⇒ Object



6271
6272
6273
# File 'lib/syntax_tree/node.rb', line 6271

def deconstruct_keys(_keys)
  { target: target, value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
# File 'lib/syntax_tree/node.rb', line 6275

def format(q)
  q.group do
    q.group { q.format(target) }
    q.text(" =")
    q.indent do
      q.breakable
      q.format(value)
    end
  end
end