Class: SyntaxTree::Assign
Overview
Assign represents assigning something to a variable or constant. Generally, the left side of the assignment is going to be any node that ends with the name “Field”.
variable = value
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#target ⇒ Object
readonly
- ARefField | ConstPathField | Field | TopConstField | VarField
-
the target to assign the result of the expression to.
-
#value ⇒ Object
readonly
- untyped
-
the expression to be assigned.
Instance Method Summary collapse
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(target:, value:, location:, comments: []) ⇒ Assign
constructor
A new instance of Assign.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(target:, value:, location:, comments: []) ⇒ Assign
Returns a new instance of Assign.
1339 1340 1341 1342 1343 1344 |
# File 'lib/syntax_tree/node.rb', line 1339 def initialize(target:, value:, location:, comments: []) @target = target @value = value @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1337 1338 1339 |
# File 'lib/syntax_tree/node.rb', line 1337 def comments @comments end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
1334 1335 1336 |
# File 'lib/syntax_tree/node.rb', line 1334 def location @location end |
#target ⇒ Object (readonly)
- ARefField | ConstPathField | Field | TopConstField | VarField
-
the target
to assign the result of the expression to
1328 1329 1330 |
# File 'lib/syntax_tree/node.rb', line 1328 def target @target end |
#value ⇒ Object (readonly)
- untyped
-
the expression to be assigned
1331 1332 1333 |
# File 'lib/syntax_tree/node.rb', line 1331 def value @value end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
1346 1347 1348 |
# File 'lib/syntax_tree/node.rb', line 1346 def child_nodes [target, value] end |
#deconstruct_keys(keys) ⇒ Object
1352 1353 1354 |
# File 'lib/syntax_tree/node.rb', line 1352 def deconstruct_keys(keys) { target: target, value: value, location: location, comments: comments } end |
#format(q) ⇒ Object
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 |
# File 'lib/syntax_tree/node.rb', line 1356 def format(q) q.group do q.format(target) q.text(" =") if skip_indent? q.text(" ") q.format(value) else q.indent do q.breakable q.format(value) end end end end |
#pretty_print(q) ⇒ Object
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 |
# File 'lib/syntax_tree/node.rb', line 1373 def pretty_print(q) q.group(2, "(", ")") do q.text("assign") q.breakable q.pp(target) q.breakable q.pp(value) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
1387 1388 1389 1390 1391 1392 1393 1394 1395 |
# File 'lib/syntax_tree/node.rb', line 1387 def to_json(*opts) { type: :assign, target: target, value: value, loc: location, cmts: comments }.to_json(*opts) end |