Class: SyntaxTree::Assoc
Overview
Assoc represents a key-value pair within a hash. It is a child node of either an AssocListFromArgs or a BareAssocHash.
{ key1: value1, key2: value2 }
In the above example, the would be two AssocNew nodes.
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#key ⇒ Object
readonly
- untyped
-
the key of this pair.
-
#value ⇒ Object
readonly
- untyped
-
the value of this pair.
Attributes inherited from Node
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(key:, value:, location:, comments: []) ⇒ Assoc
constructor
A new instance of Assoc.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(key:, value:, location:, comments: []) ⇒ Assoc
Returns a new instance of Assoc.
1274 1275 1276 1277 1278 1279 |
# File 'lib/syntax_tree/node.rb', line 1274 def initialize(key:, value:, location:, comments: []) @key = key @value = value @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1272 1273 1274 |
# File 'lib/syntax_tree/node.rb', line 1272 def comments @comments end |
#key ⇒ Object (readonly)
- untyped
-
the key of this pair
1266 1267 1268 |
# File 'lib/syntax_tree/node.rb', line 1266 def key @key end |
#value ⇒ Object (readonly)
- untyped
-
the value of this pair
1269 1270 1271 |
# File 'lib/syntax_tree/node.rb', line 1269 def value @value end |
Instance Method Details
#accept(visitor) ⇒ Object
1281 1282 1283 |
# File 'lib/syntax_tree/node.rb', line 1281 def accept(visitor) visitor.visit_assoc(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
1285 1286 1287 |
# File 'lib/syntax_tree/node.rb', line 1285 def child_nodes [key, value] end |
#deconstruct_keys(_keys) ⇒ Object
1291 1292 1293 |
# File 'lib/syntax_tree/node.rb', line 1291 def deconstruct_keys(_keys) { key: key, value: value, location: location, comments: comments } end |
#format(q) ⇒ Object
1295 1296 1297 1298 1299 1300 1301 |
# File 'lib/syntax_tree/node.rb', line 1295 def format(q) if value.is_a?(HashLiteral) format_contents(q) else q.group { format_contents(q) } end end |