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
-
#assocs ⇒ Object
readonly
attr_reader assocs: Array.
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location?.
-
#constant ⇒ Object
readonly
attr_reader constant: Node?.
-
#kwrest ⇒ Object
readonly
attr_reader kwrest: Node?.
-
#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) -> HashPatternNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(constant, assocs, kwrest, opening_loc, closing_loc, location) ⇒ HashPatternNode
constructor
def initialize: (constant: Node?, assocs: Array, kwrest: Node?, opening_loc: Location?, 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(constant, assocs, kwrest, opening_loc, closing_loc, location) ⇒ HashPatternNode
def initialize: (constant: Node?, assocs: Array, kwrest: Node?, opening_loc: Location?, closing_loc: Location?, location: Location) -> void
6648 6649 6650 6651 6652 6653 6654 6655 |
# File 'lib/prism/node.rb', line 6648 def initialize(constant, assocs, kwrest, opening_loc, closing_loc, location) @constant = constant @assocs = assocs @kwrest = kwrest @opening_loc = opening_loc @closing_loc = closing_loc @location = location end |
Instance Attribute Details
#assocs ⇒ Object (readonly)
attr_reader assocs: Array
6636 6637 6638 |
# File 'lib/prism/node.rb', line 6636 def assocs @assocs end |
#closing_loc ⇒ Object (readonly)
attr_reader closing_loc: Location?
6645 6646 6647 |
# File 'lib/prism/node.rb', line 6645 def closing_loc @closing_loc end |
#constant ⇒ Object (readonly)
attr_reader constant: Node?
6633 6634 6635 |
# File 'lib/prism/node.rb', line 6633 def constant @constant end |
#kwrest ⇒ Object (readonly)
attr_reader kwrest: Node?
6639 6640 6641 |
# File 'lib/prism/node.rb', line 6639 def kwrest @kwrest end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location?
6642 6643 6644 |
# File 'lib/prism/node.rb', line 6642 def opening_loc @opening_loc end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
6658 6659 6660 |
# File 'lib/prism/node.rb', line 6658 def accept(visitor) visitor.visit_hash_pattern_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
6663 6664 6665 |
# File 'lib/prism/node.rb', line 6663 def child_nodes [constant, *assocs, kwrest] end |
#closing ⇒ Object
def closing: () -> String?
6707 6708 6709 |
# File 'lib/prism/node.rb', line 6707 def closing closing_loc&.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
6677 6678 6679 |
# File 'lib/prism/node.rb', line 6677 def comment_targets [*constant, *assocs, *kwrest, *opening_loc, *closing_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
6668 6669 6670 6671 6672 6673 6674 |
# File 'lib/prism/node.rb', line 6668 def compact_child_nodes compact = [] compact << constant if constant compact.concat(assocs) compact << kwrest if kwrest compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> HashPatternNode
6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 |
# File 'lib/prism/node.rb', line 6682 def copy(**params) HashPatternNode.new( params.fetch(:constant) { constant }, params.fetch(:assocs) { assocs }, params.fetch(:kwrest) { kwrest }, params.fetch(:opening_loc) { opening_loc }, params.fetch(:closing_loc) { closing_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
6697 6698 6699 |
# File 'lib/prism/node.rb', line 6697 def deconstruct_keys(keys) { constant: constant, assocs: assocs, kwrest: kwrest, opening_loc: opening_loc, closing_loc: closing_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 |
# File 'lib/prism/node.rb', line 6711 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 << "├── assocs: #{inspector.list("#{inspector.prefix}│ ", assocs)}" if (kwrest = self.kwrest).nil? inspector << "├── kwrest: ∅\n" else inspector << "├── kwrest:\n" inspector << kwrest.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?
6702 6703 6704 |
# File 'lib/prism/node.rb', line 6702 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
6745 6746 6747 |
# File 'lib/prism/node.rb', line 6745 def type :hash_pattern_node end |