Sha256: dae99de4447ab0857d5b94c0fbb57233780c538d1f4724a1b104a6a5597a88d5

Contents?: true

Size: 928 Bytes

Versions: 10

Compression:

Stored size: 928 Bytes

Contents

module R10K
  module Util
    module SymbolizeKeys
      module_function

      # Convert all String keys to Symbol keys
      #
      # @param hash [Hash] The data structure to convert
      # @param recurse [Boolean] Whether to recursively symbolize keys in nested
      #   hash values. Defaults to false.
      # @raise [TypeError] If a String key collides with an existing Symbol key
      # @return [void]
      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"
            end
            hash[key.to_sym] = hash.delete(key)
            key = key.to_sym
          end

          value = hash[key]
          if recurse && value.is_a?(Hash)
            symbolize_keys!(value, true)
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
r10k-2.2.2 lib/r10k/util/symbolize_keys.rb
r10k-2.2.1 lib/r10k/util/symbolize_keys.rb
r10k-2.2.0 lib/r10k/util/symbolize_keys.rb
r10k-2.1.1 lib/r10k/util/symbolize_keys.rb
r10k-2.1.0 lib/r10k/util/symbolize_keys.rb
r10k-2.0.3 lib/r10k/util/symbolize_keys.rb
r10k-2.0.2 lib/r10k/util/symbolize_keys.rb
r10k-2.0.1 lib/r10k/util/symbolize_keys.rb
r10k-2.0.0 lib/r10k/util/symbolize_keys.rb
r10k-1.5.1 lib/r10k/util/symbolize_keys.rb