Class: Prism::MatchRequiredNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents the use of the ‘=>` operator.

foo => bar
^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

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_locObject (readonly)

attr_reader operator_loc: Location



9646
9647
9648
# File 'lib/prism/node.rb', line 9646

def operator_loc
  @operator_loc
end

#patternObject (readonly)

attr_reader pattern: Node



9643
9644
9645
# File 'lib/prism/node.rb', line 9643

def pattern
  @pattern
end

#valueObject (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_nodesObject 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_targetsObject

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_nodesObject

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

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



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

#operatorObject

def operator: () -> String



9695
9696
9697
# File 'lib/prism/node.rb', line 9695

def operator
  operator_loc.slice
end

#typeObject

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