Class: Prism::MatchRequiredNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::MatchRequiredNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents the use of the ‘=>` operator.
foo => bar
^^^^^^^^^^
Instance Attribute Summary collapse
-
#operator_loc ⇒ Object
readonly
attr_reader operator_loc: Location.
-
#pattern ⇒ Object
readonly
attr_reader pattern: Node.
-
#value ⇒ Object
readonly
attr_reader value: Node.
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) -> MatchRequiredNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(value, pattern, operator_loc, location) ⇒ MatchRequiredNode
constructor
def initialize: (value: Node, pattern: Node, operator_loc: Location, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
-
#operator ⇒ Object
def operator: () -> 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(value, pattern, operator_loc, location) ⇒ MatchRequiredNode
def initialize: (value: Node, pattern: Node, operator_loc: Location, location: Location) -> void
9649 9650 9651 9652 9653 9654 |
# File 'lib/prism/node.rb', line 9649 def initialize(value, pattern, operator_loc, location) @value = value @pattern = pattern @operator_loc = operator_loc @location = location end |
Instance Attribute Details
#operator_loc ⇒ Object (readonly)
attr_reader operator_loc: Location
9646 9647 9648 |
# File 'lib/prism/node.rb', line 9646 def operator_loc @operator_loc end |
#pattern ⇒ Object (readonly)
attr_reader pattern: Node
9643 9644 9645 |
# File 'lib/prism/node.rb', line 9643 def pattern @pattern end |
#value ⇒ Object (readonly)
attr_reader value: Node
9640 9641 9642 |
# File 'lib/prism/node.rb', line 9640 def value @value end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
9657 9658 9659 |
# File 'lib/prism/node.rb', line 9657 def accept(visitor) visitor.visit_match_required_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
9662 9663 9664 |
# File 'lib/prism/node.rb', line 9662 def child_nodes [value, pattern] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
9672 9673 9674 |
# File 'lib/prism/node.rb', line 9672 def comment_targets [value, pattern, operator_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
9667 9668 9669 |
# File 'lib/prism/node.rb', line 9667 def compact_child_nodes [value, pattern] end |
#copy(**params) ⇒ Object
def copy: (**params) -> MatchRequiredNode
9677 9678 9679 9680 9681 9682 9683 9684 |
# File 'lib/prism/node.rb', line 9677 def copy(**params) MatchRequiredNode.new( params.fetch(:value) { value }, params.fetch(:pattern) { pattern }, params.fetch(:operator_loc) { operator_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
9690 9691 9692 |
# File 'lib/prism/node.rb', line 9690 def deconstruct_keys(keys) { value: value, pattern: pattern, operator_loc: operator_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
9699 9700 9701 9702 9703 9704 9705 9706 9707 |
# File 'lib/prism/node.rb', line 9699 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── value:\n" inspector << inspector.child_node(value, "│ ") inspector << "├── pattern:\n" inspector << inspector.child_node(pattern, "│ ") inspector << "└── operator_loc: #{inspector.location(operator_loc)}\n" inspector.to_str end |
#operator ⇒ Object
def operator: () -> String
9695 9696 9697 |
# File 'lib/prism/node.rb', line 9695 def operator operator_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
9723 9724 9725 |
# File 'lib/prism/node.rb', line 9723 def type :match_required_node end |