Module: R10K::Util::SymbolizeKeys

Defined in:
lib/r10k/util/symbolize_keys.rb

Class Method Summary collapse

Class Method Details

.symbolize_keys!(hash, recurse = false) ⇒ void

This method returns an undefined value.

Convert all String keys to Symbol keys

Parameters:

  • hash (Hash)

    The data structure to convert

  • recurse (Boolean) (defaults to: false)

    Whether to recursively symbolize keys in nested hash values. Defaults to false.

Raises:

  • (TypeError)

    If a String key collides with an existing Symbol key



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/r10k/util/symbolize_keys.rb', line 13

def symbolize_keys!(hash, recurse = false)
  hash.keys.each do |key|
    if key.is_a?(String)
      if hash.key?(key.to_sym)
        raise TypeError, _("An existing interned key for %{key} exists, cannot overwrite") % {key: key}
      end
      hash[key.to_sym] = hash.delete(key)
      key = key.to_sym
    end

    value = hash[key]
    if recurse
      if value.is_a?(Hash)
        symbolize_keys!(value, true)
      elsif value.is_a?(Array)
        value.map { |item| symbolize_keys!(item, true) if item.is_a?(Hash) }
      end
    end
  end
end