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
Constructor Details
#initialize(key:, value:, location:, comments: []) ⇒ Assoc
Returns a new instance of Assoc.
1171 1172 1173 1174 1175 1176 |
# File 'lib/syntax_tree/node.rb', line 1171 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
1169 1170 1171 |
# File 'lib/syntax_tree/node.rb', line 1169 def comments @comments end |
#key ⇒ Object (readonly)
- untyped
-
the key of this pair
1163 1164 1165 |
# File 'lib/syntax_tree/node.rb', line 1163 def key @key end |
#value ⇒ Object (readonly)
- untyped
-
the value of this pair
1166 1167 1168 |
# File 'lib/syntax_tree/node.rb', line 1166 def value @value end |
Instance Method Details
#accept(visitor) ⇒ Object
1178 1179 1180 |
# File 'lib/syntax_tree/node.rb', line 1178 def accept(visitor) visitor.visit_assoc(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
1182 1183 1184 |
# File 'lib/syntax_tree/node.rb', line 1182 def child_nodes [key, value] end |
#deconstruct_keys(keys) ⇒ Object
1188 1189 1190 |
# File 'lib/syntax_tree/node.rb', line 1188 def deconstruct_keys(keys) { key: key, value: value, location: location, comments: comments } end |
#format(q) ⇒ Object
1192 1193 1194 1195 1196 1197 1198 |
# File 'lib/syntax_tree/node.rb', line 1192 def format(q) if value&.is_a?(HashLiteral) format_contents(q) else q.group { format_contents(q) } end end |