Class: Prism::AssocNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::AssocNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a hash key/value pair.
{ a => b }
^^^^^^
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
attr_reader key: Node.
-
#operator_loc ⇒ Object
readonly
attr_reader operator_loc: Location?.
-
#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) -> AssocNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(key, value, operator_loc, location) ⇒ AssocNode
constructor
def initialize: (key: Node, value: 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(key, value, operator_loc, location) ⇒ AssocNode
def initialize: (key: Node, value: Node?, operator_loc: Location?, location: Location) -> void
747 748 749 750 751 752 |
# File 'lib/prism/node.rb', line 747 def initialize(key, value, operator_loc, location) @key = key @value = value @operator_loc = operator_loc @location = location end |
Instance Attribute Details
#key ⇒ Object (readonly)
attr_reader key: Node
738 739 740 |
# File 'lib/prism/node.rb', line 738 def key @key end |
#operator_loc ⇒ Object (readonly)
attr_reader operator_loc: Location?
744 745 746 |
# File 'lib/prism/node.rb', line 744 def operator_loc @operator_loc end |
#value ⇒ Object (readonly)
attr_reader value: Node?
741 742 743 |
# File 'lib/prism/node.rb', line 741 def value @value end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
755 756 757 |
# File 'lib/prism/node.rb', line 755 def accept(visitor) visitor.visit_assoc_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
760 761 762 |
# File 'lib/prism/node.rb', line 760 def child_nodes [key, value] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
773 774 775 |
# File 'lib/prism/node.rb', line 773 def comment_targets [key, *value, *operator_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
765 766 767 768 769 770 |
# File 'lib/prism/node.rb', line 765 def compact_child_nodes compact = [] compact << key compact << value if value compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> AssocNode
778 779 780 781 782 783 784 785 |
# File 'lib/prism/node.rb', line 778 def copy(**params) AssocNode.new( params.fetch(:key) { key }, params.fetch(:value) { value }, params.fetch(:operator_loc) { operator_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
791 792 793 |
# File 'lib/prism/node.rb', line 791 def deconstruct_keys(keys) { key: key, value: value, operator_loc: operator_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
800 801 802 803 804 805 806 807 808 809 810 811 812 |
# File 'lib/prism/node.rb', line 800 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── key:\n" inspector << inspector.child_node(key, "│ ") if (value = self.value).nil? inspector << "├── value: ∅\n" else inspector << "├── value:\n" inspector << value.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end inspector << "└── operator_loc: #{inspector.location(operator_loc)}\n" inspector.to_str end |
#operator ⇒ Object
def operator: () -> String?
796 797 798 |
# File 'lib/prism/node.rb', line 796 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
828 829 830 |
# File 'lib/prism/node.rb', line 828 def type :assoc_node end |