Class: Prism::KeywordHashNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::KeywordHashNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a hash literal without opening and closing braces.
foo(a: b)
^^^^
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
attr_reader elements: Array.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> KeywordHashNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(elements, location) ⇒ KeywordHashNode
constructor
def initialize: (elements: Array, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
-
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
Constructor Details
#initialize(elements, location) ⇒ KeywordHashNode
def initialize: (elements: Array, location: Location) -> void
8407 8408 8409 8410 |
# File 'lib/prism/node.rb', line 8407 def initialize(elements, location) @elements = elements @location = location end |
Instance Attribute Details
#elements ⇒ Object (readonly)
attr_reader elements: Array
8404 8405 8406 |
# File 'lib/prism/node.rb', line 8404 def elements @elements end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
8413 8414 8415 |
# File 'lib/prism/node.rb', line 8413 def accept(visitor) visitor.visit_keyword_hash_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
8418 8419 8420 |
# File 'lib/prism/node.rb', line 8418 def child_nodes [*elements] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
8428 8429 8430 |
# File 'lib/prism/node.rb', line 8428 def comment_targets [*elements] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
8423 8424 8425 |
# File 'lib/prism/node.rb', line 8423 def compact_child_nodes [*elements] end |
#copy(**params) ⇒ Object
def copy: (**params) -> KeywordHashNode
8433 8434 8435 8436 8437 8438 |
# File 'lib/prism/node.rb', line 8433 def copy(**params) KeywordHashNode.new( params.fetch(:elements) { elements }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
8444 8445 8446 |
# File 'lib/prism/node.rb', line 8444 def deconstruct_keys(keys) { elements: elements, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
8448 8449 8450 8451 8452 |
# File 'lib/prism/node.rb', line 8448 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "└── elements: #{inspector.list("#{inspector.prefix} ", elements)}" inspector.to_str end |
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.
Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.
def type: () -> Symbol
8468 8469 8470 |
# File 'lib/prism/node.rb', line 8468 def type :keyword_hash_node end |