class RuboCop::AST::HashNode

to all ‘hash` nodes within RuboCop.
node when the builder constructs the AST, making its methods available
A node extension for `hash` nodes. This will be used in place of a plain

def braces?

Returns:
  • (Boolean) - whether the `hash` literal is enclosed in braces
def braces?
  loc.end&.is?('}')
end

def each_key(&block)

Returns:
  • (Enumerator) - if no block is given
  • (self) - if a block is given

Other tags:
    Note: - `kwsplat` nodes are ignored.
def each_key(&block)
  return pairs.map(&:key).to_enum unless block
  pairs.map(&:key).each(&block)
  self
end

def each_pair

Returns:
  • (Enumerator) - if no block is given
  • (self) - if a block is given

Other tags:
    Note: - `kwsplat` nodes are ignored.
def each_pair
  return each_child_node(:pair).to_enum unless block_given?
  each_child_node(:pair) do |pair|
    yield(*pair)
  end
  self
end

def each_value(&block)

Returns:
  • (Enumerator) - if no block is given
  • (self) - if a block is given

Other tags:
    Note: - `kwsplat` nodes are ignored.
def each_value(&block)
  return pairs.map(&:value).to_enum unless block
  pairs.map(&:value).each(&block)
  self
end

def empty?

@return[Boolean] whether the `hash` is empty

Checks whether the `hash` node contains any `pair`- or `kwsplat` nodes.
def empty?
  children.empty?
end

def keys

Returns:
  • (Array) - an array of keys in the `hash` literal

Other tags:
    Note: - `kwsplat` nodes are ignored.
def keys
  each_key.to_a
end

def mixed_delimiters?

Returns:
  • (Boolean) - whether the `hash` uses mixed delimiters

Other tags:
    Note: - `kwsplat` nodes are ignored.
def mixed_delimiters?
  pairs.map(&:delimiter).uniq.size > 1
end

def pairs

Returns:
  • (Array) - an array of `pair` nodes

Other tags:
    Note: - this may be different from children as `kwsplat` nodes are
def pairs
  each_pair.to_a
end

def pairs_on_same_line?

Returns:
  • (Boolean) - whether any `pair` nodes are on the same line

Other tags:
    Note: - `kwsplat` nodes are ignored.
    Note: - A multiline `pair` is considered to be on the same line if it
def pairs_on_same_line?
  pairs.each_cons(2).any? { |first, second| first.same_line?(second) }
end

def values

Returns:
  • (Array) - an array of values in the `hash` literal

Other tags:
    Note: - `kwsplat` nodes are ignored.
def values
  each_pair.map(&:value)
end