Class: Prism::HashPatternNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::HashPatternNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a hash pattern in pattern matching.
foo => { a: 1, b: 2 }
^^^^^^^^^^^^^^
foo => { a: 1, b: 2, **c }
^^^^^^^^^^^^^^^^^^^
Instance Attribute Summary collapse
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location?.
-
#constant ⇒ Object
readonly
attr_reader constant: Node?.
-
#elements ⇒ Object
readonly
attr_reader elements: Array.
-
#opening_loc ⇒ Object
readonly
attr_reader opening_loc: Location?.
-
#rest ⇒ Object
readonly
attr_reader rest: Node?.
Class Method Summary collapse
-
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain.
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) -> HashPatternNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(constant, elements, rest, opening_loc, closing_loc, location) ⇒ HashPatternNode
constructor
def initialize: (constant: Node?, elements: Array, rest: Node?, opening_loc: Location?, closing_loc: Location?, location: Location) -> void.
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#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(constant, elements, rest, opening_loc, closing_loc, location) ⇒ HashPatternNode
def initialize: (constant: Node?, elements: Array, rest: Node?, opening_loc: Location?, closing_loc: Location?, location: Location) -> void
7593 7594 7595 7596 7597 7598 7599 7600 |
# File 'lib/prism/node.rb', line 7593 def initialize(constant, elements, rest, opening_loc, closing_loc, location) @constant = constant @elements = elements @rest = rest @opening_loc = opening_loc @closing_loc = closing_loc @location = location end |
Instance Attribute Details
#closing_loc ⇒ Object (readonly)
attr_reader closing_loc: Location?
7590 7591 7592 |
# File 'lib/prism/node.rb', line 7590 def closing_loc @closing_loc end |
#constant ⇒ Object (readonly)
attr_reader constant: Node?
7578 7579 7580 |
# File 'lib/prism/node.rb', line 7578 def constant @constant end |
#elements ⇒ Object (readonly)
attr_reader elements: Array
7581 7582 7583 |
# File 'lib/prism/node.rb', line 7581 def elements @elements end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location?
7587 7588 7589 |
# File 'lib/prism/node.rb', line 7587 def opening_loc @opening_loc end |
#rest ⇒ Object (readonly)
attr_reader rest: Node?
7584 7585 7586 |
# File 'lib/prism/node.rb', line 7584 def rest @rest end |
Class Method Details
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.
def self.type: () -> Symbol
7701 7702 7703 |
# File 'lib/prism/node.rb', line 7701 def self.type :hash_pattern_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
7603 7604 7605 |
# File 'lib/prism/node.rb', line 7603 def accept(visitor) visitor.visit_hash_pattern_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
7608 7609 7610 |
# File 'lib/prism/node.rb', line 7608 def child_nodes [constant, *elements, rest] end |
#closing ⇒ Object
def closing: () -> String?
7652 7653 7654 |
# File 'lib/prism/node.rb', line 7652 def closing closing_loc&.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
7622 7623 7624 |
# File 'lib/prism/node.rb', line 7622 def comment_targets [*constant, *elements, *rest, *opening_loc, *closing_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
7613 7614 7615 7616 7617 7618 7619 |
# File 'lib/prism/node.rb', line 7613 def compact_child_nodes compact = [] compact << constant if constant compact.concat(elements) compact << rest if rest compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> HashPatternNode
7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 |
# File 'lib/prism/node.rb', line 7627 def copy(**params) HashPatternNode.new( params.fetch(:constant) { constant }, params.fetch(:elements) { elements }, params.fetch(:rest) { rest }, params.fetch(:opening_loc) { opening_loc }, params.fetch(:closing_loc) { closing_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
7642 7643 7644 |
# File 'lib/prism/node.rb', line 7642 def deconstruct_keys(keys) { constant: constant, elements: elements, rest: rest, opening_loc: opening_loc, closing_loc: closing_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 |
# File 'lib/prism/node.rb', line 7657 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) if (constant = self.constant).nil? inspector << "├── constant: ∅\n" else inspector << "├── constant:\n" inspector << constant.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end inspector << "├── elements: #{inspector.list("#{inspector.prefix}│ ", elements)}" if (rest = self.rest).nil? inspector << "├── rest: ∅\n" else inspector << "├── rest:\n" inspector << rest.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n" inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n" inspector.to_str end |
#opening ⇒ Object
def opening: () -> String?
7647 7648 7649 |
# File 'lib/prism/node.rb', line 7647 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
7691 7692 7693 |
# File 'lib/prism/node.rb', line 7691 def type :hash_pattern_node end |