Class: Prism::ArrayPatternNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::ArrayPatternNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents an array pattern in pattern matching.
foo in 1, 2
^^^^^^^^^^^
foo in [1, 2]
^^^^^^^^^^^^^
foo in *1
^^^^^^^^^
foo in Bar[]
^^^^^^^^^^^^
foo in Bar[1, 2, 3]
^^^^^^^^^^^^^^^^^^^
Instance Attribute Summary collapse
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location?.
-
#constant ⇒ Object
readonly
attr_reader constant: Node?.
-
#opening_loc ⇒ Object
readonly
attr_reader opening_loc: Location?.
-
#posts ⇒ Object
readonly
attr_reader posts: Array.
-
#requireds ⇒ Object
readonly
attr_reader requireds: Array.
-
#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) -> ArrayPatternNode.
- #deconstruct_keys(keys) ⇒ Object
- #initialize(constant, requireds, rest, posts, opening_loc, closing_loc, location) ⇒ ArrayPatternNode constructor
-
#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, requireds, rest, posts, opening_loc, closing_loc, location) ⇒ ArrayPatternNode
711 712 713 714 715 716 717 718 719 |
# File 'lib/prism/node.rb', line 711 def initialize(constant, requireds, rest, posts, opening_loc, closing_loc, location) @constant = constant @requireds = requireds @rest = rest @posts = posts @opening_loc = opening_loc @closing_loc = closing_loc @location = location end |
Instance Attribute Details
#closing_loc ⇒ Object (readonly)
attr_reader closing_loc: Location?
708 709 710 |
# File 'lib/prism/node.rb', line 708 def closing_loc @closing_loc end |
#constant ⇒ Object (readonly)
attr_reader constant: Node?
693 694 695 |
# File 'lib/prism/node.rb', line 693 def constant @constant end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location?
705 706 707 |
# File 'lib/prism/node.rb', line 705 def opening_loc @opening_loc end |
#posts ⇒ Object (readonly)
attr_reader posts: Array
702 703 704 |
# File 'lib/prism/node.rb', line 702 def posts @posts end |
#requireds ⇒ Object (readonly)
attr_reader requireds: Array
696 697 698 |
# File 'lib/prism/node.rb', line 696 def requireds @requireds end |
#rest ⇒ Object (readonly)
attr_reader rest: Node?
699 700 701 |
# File 'lib/prism/node.rb', line 699 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
823 824 825 |
# File 'lib/prism/node.rb', line 823 def self.type :array_pattern_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
722 723 724 |
# File 'lib/prism/node.rb', line 722 def accept(visitor) visitor.visit_array_pattern_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
727 728 729 |
# File 'lib/prism/node.rb', line 727 def child_nodes [constant, *requireds, rest, *posts] end |
#closing ⇒ Object
def closing: () -> String?
773 774 775 |
# File 'lib/prism/node.rb', line 773 def closing closing_loc&.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
742 743 744 |
# File 'lib/prism/node.rb', line 742 def comment_targets [*constant, *requireds, *rest, *posts, *opening_loc, *closing_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
732 733 734 735 736 737 738 739 |
# File 'lib/prism/node.rb', line 732 def compact_child_nodes compact = [] compact << constant if constant compact.concat(requireds) compact << rest if rest compact.concat(posts) compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> ArrayPatternNode
747 748 749 750 751 752 753 754 755 756 757 |
# File 'lib/prism/node.rb', line 747 def copy(**params) ArrayPatternNode.new( params.fetch(:constant) { constant }, params.fetch(:requireds) { requireds }, params.fetch(:rest) { rest }, params.fetch(:posts) { posts }, params.fetch(:opening_loc) { opening_loc }, params.fetch(:closing_loc) { closing_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
763 764 765 |
# File 'lib/prism/node.rb', line 763 def deconstruct_keys(keys) { constant: constant, requireds: requireds, rest: rest, posts: posts, opening_loc: opening_loc, closing_loc: closing_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 |
# File 'lib/prism/node.rb', line 778 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 << "├── requireds: #{inspector.list("#{inspector.prefix}│ ", requireds)}" if (rest = self.rest).nil? inspector << "├── rest: ∅\n" else inspector << "├── rest:\n" inspector << rest.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end inspector << "├── posts: #{inspector.list("#{inspector.prefix}│ ", posts)}" 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?
768 769 770 |
# File 'lib/prism/node.rb', line 768 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
813 814 815 |
# File 'lib/prism/node.rb', line 813 def type :array_pattern_node end |