Class: Prism::KeywordHashNode

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

Overview

Represents a hash literal without opening and closing braces.

foo(a: b)
    ^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new KeywordHashNode node.

: (Source source, Integer node_id, Location location, Integer flags, Array[AssocNode | AssocSplatNode] elements) -> void

Parameters:



20764
20765
20766
20767
20768
20769
20770
# File 'lib/prism/node.rb', line 20764

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

Class Method Details

.type ⇒ :keyword_hash_node

See Node.type.

: () -> :keyword_hash_node

Returns:

  • (:keyword_hash_node)


20849
20850
20851
# File 'lib/prism/node.rb', line 20849

def self.type
  :keyword_hash_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


20883
20884
20885
20886
20887
20888
# File 'lib/prism/node.rb', line 20883

def ===(other) # :nodoc:
  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

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


20786
20787
20788
# File 'lib/prism/node.rb', line 20786

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


20793
20794
20795
# File 'lib/prism/node.rb', line 20793

def child_nodes
  [*elements]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


20810
20811
20812
# File 'lib/prism/node.rb', line 20810

def compact_child_nodes
  [*elements]
end

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

:call-seq:

copy(**fields) -> KeywordHashNode

Creates a copy of self with the given fields, using self as the template.

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

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • elements: (Array[AssocNode | AssocSplatNode]) (defaults to: self.elements)

Returns:



20820
20821
20822
# File 'lib/prism/node.rb', line 20820

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) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
# File 'lib/prism/node.rb', line 20827

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location elements]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :elements                then deconstructed[:elements] = self.elements
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


20801
20802
20803
20804
20805
# File 'lib/prism/node.rb', line 20801

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  elements.each { |node| yield node }
end

#elements ⇒ Array[AssocNode | AssocSplatNode]

:call-seq:

elements -> Array[AssocNode | AssocSplatNode]

Returns the elements attribute.

: () -> Array[AssocNode | AssocSplatNode]

Returns:



20874
20875
20876
# File 'lib/prism/node.rb', line 20874

def elements
  @elements
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


20854
20855
20856
# File 'lib/prism/node.rb', line 20854

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#symbol_keys? ⇒ Boolean

:category: Flags a keyword hash which only has AssocNode elements all with symbol keys, which means the elements can be treated as keyword arguments

: () -> bool

Returns:

  • (Boolean)


20864
20865
20866
# File 'lib/prism/node.rb', line 20864

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

#type ⇒ :keyword_hash_node

See Node#type.

: () -> :keyword_hash_node

Returns:

  • (:keyword_hash_node)


20842
20843
20844
# File 'lib/prism/node.rb', line 20842

def type
  :keyword_hash_node
end