Class: Prism::KeywordHashNode

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

Overview

Represents a hash literal without opening and closing braces.

foo(a: b)
    ^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements, location) ⇒ KeywordHashNode

def initialize: (elements: Array, location: Location) -> void



8407
8408
8409
8410
# File 'lib/prism/node.rb', line 8407

def initialize(elements, location)
  @elements = elements
  @location = location
end

Instance Attribute Details

#elementsObject (readonly)

attr_reader elements: Array



8404
8405
8406
# File 'lib/prism/node.rb', line 8404

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



8413
8414
8415
# File 'lib/prism/node.rb', line 8413

def accept(visitor)
  visitor.visit_keyword_hash_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



8418
8419
8420
# File 'lib/prism/node.rb', line 8418

def child_nodes
  [*elements]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



8428
8429
8430
# File 'lib/prism/node.rb', line 8428

def comment_targets
  [*elements]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8423
8424
8425
# File 'lib/prism/node.rb', line 8423

def compact_child_nodes
  [*elements]
end

#copy(**params) ⇒ Object

def copy: (**params) -> KeywordHashNode



8433
8434
8435
8436
8437
8438
# File 'lib/prism/node.rb', line 8433

def copy(**params)
  KeywordHashNode.new(
    params.fetch(:elements) { elements },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



8444
8445
8446
# File 'lib/prism/node.rb', line 8444

def deconstruct_keys(keys)
  { elements: elements, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



8448
8449
8450
8451
8452
# File 'lib/prism/node.rb', line 8448

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "└── elements: #{inspector.list("#{inspector.prefix}    ", elements)}"
  inspector.to_str
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



8468
8469
8470
# File 'lib/prism/node.rb', line 8468

def type
  :keyword_hash_node
end