Class: Prism::HashNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::HashNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a hash literal.
{ a => b }
^^^^^^^^^^
Instance Attribute Summary collapse
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location.
-
#elements ⇒ Object
readonly
attr_reader elements: Array.
-
#opening_loc ⇒ Object
readonly
attr_reader opening_loc: Location.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#closing ⇒ Object
def closing: () -> String.
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> HashNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(opening_loc, elements, closing_loc, location) ⇒ HashNode
constructor
def initialize: (opening_loc: Location, elements: Array, closing_loc: Location, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
-
#opening ⇒ Object
def opening: () -> String.
-
#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(opening_loc, elements, closing_loc, location) ⇒ HashNode
def initialize: (opening_loc: Location, elements: Array, closing_loc: Location, location: Location) -> void
6542 6543 6544 6545 6546 6547 |
# File 'lib/prism/node.rb', line 6542 def initialize(opening_loc, elements, closing_loc, location) @opening_loc = opening_loc @elements = elements @closing_loc = closing_loc @location = location end |
Instance Attribute Details
#closing_loc ⇒ Object (readonly)
attr_reader closing_loc: Location
6539 6540 6541 |
# File 'lib/prism/node.rb', line 6539 def closing_loc @closing_loc end |
#elements ⇒ Object (readonly)
attr_reader elements: Array
6536 6537 6538 |
# File 'lib/prism/node.rb', line 6536 def elements @elements end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location
6533 6534 6535 |
# File 'lib/prism/node.rb', line 6533 def opening_loc @opening_loc end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
6550 6551 6552 |
# File 'lib/prism/node.rb', line 6550 def accept(visitor) visitor.visit_hash_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
6555 6556 6557 |
# File 'lib/prism/node.rb', line 6555 def child_nodes [*elements] end |
#closing ⇒ Object
def closing: () -> String
6593 6594 6595 |
# File 'lib/prism/node.rb', line 6593 def closing closing_loc.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
6565 6566 6567 |
# File 'lib/prism/node.rb', line 6565 def comment_targets [opening_loc, *elements, closing_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
6560 6561 6562 |
# File 'lib/prism/node.rb', line 6560 def compact_child_nodes [*elements] end |
#copy(**params) ⇒ Object
def copy: (**params) -> HashNode
6570 6571 6572 6573 6574 6575 6576 6577 |
# File 'lib/prism/node.rb', line 6570 def copy(**params) HashNode.new( params.fetch(:opening_loc) { opening_loc }, params.fetch(:elements) { elements }, params.fetch(:closing_loc) { closing_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
6583 6584 6585 |
# File 'lib/prism/node.rb', line 6583 def deconstruct_keys(keys) { opening_loc: opening_loc, elements: elements, closing_loc: closing_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
6597 6598 6599 6600 6601 6602 6603 |
# File 'lib/prism/node.rb', line 6597 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n" inspector << "├── elements: #{inspector.list("#{inspector.prefix}│ ", elements)}" inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n" inspector.to_str end |
#opening ⇒ Object
def opening: () -> String
6588 6589 6590 |
# File 'lib/prism/node.rb', line 6588 def opening opening_loc.slice 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
6619 6620 6621 |
# File 'lib/prism/node.rb', line 6619 def type :hash_node end |