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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, elements) ⇒ KeywordHashNode

Initialize a new KeywordHashNode node.



12146
12147
12148
12149
12150
12151
12152
# File 'lib/prism/node.rb', line 12146

def initialize(source, node_id, location, flags, elements)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @elements = elements
end

Instance Attribute Details

#elementsObject (readonly)

attr_reader elements: Array[AssocNode | AssocSplatNode]



12200
12201
12202
# File 'lib/prism/node.rb', line 12200

def elements
  @elements
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See Node::type.



12213
12214
12215
# File 'lib/prism/node.rb', line 12213

def self.type
  :keyword_hash_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



12219
12220
12221
12222
12223
12224
# File 'lib/prism/node.rb', line 12219

def ===(other)
  other.is_a?(KeywordHashNode) &&
    (flags === other.flags) &&
    (elements.length == other.elements.length) &&
    elements.zip(other.elements).all? { |left, right| left === right }
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



12155
12156
12157
# File 'lib/prism/node.rb', line 12155

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



12160
12161
12162
# File 'lib/prism/node.rb', line 12160

def child_nodes
  [*elements]
end

#comment_targetsObject

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



12177
12178
12179
# File 'lib/prism/node.rb', line 12177

def comment_targets
  [*elements] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



12172
12173
12174
# File 'lib/prism/node.rb', line 12172

def compact_child_nodes
  [*elements]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, elements: self.elements) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?elements: Array[AssocNode | AssocSplatNode]) -> KeywordHashNode



12182
12183
12184
# File 'lib/prism/node.rb', line 12182

def copy(node_id: self.node_id, location: self.location, flags: self.flags, elements: self.elements)
  KeywordHashNode.new(source, node_id, location, flags, elements)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, elements: Array[AssocNode | AssocSplatNode] }



12190
12191
12192
# File 'lib/prism/node.rb', line 12190

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

#each_child_nodeObject

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator



12165
12166
12167
12168
12169
# File 'lib/prism/node.rb', line 12165

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  elements.each { |node| yield node }
end

#inspectObject

def inspect -> String



12203
12204
12205
# File 'lib/prism/node.rb', line 12203

def inspect
  InspectVisitor.compose(self)
end

#symbol_keys?Boolean

def symbol_keys?: () -> bool



12195
12196
12197
# File 'lib/prism/node.rb', line 12195

def symbol_keys?
  flags.anybits?(KeywordHashNodeFlags::SYMBOL_KEYS)
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



12208
12209
12210
# File 'lib/prism/node.rb', line 12208

def type
  :keyword_hash_node
end