Sha256: c6842f31795bac338a73145b41202c835c470de80bedb3b0cc0d9bb4b490d8e5

Contents?: true

Size: 869 Bytes

Versions: 2

Compression:

Stored size: 869 Bytes

Contents

module Coolkit
  # Recursively convert keys in a Hash or Array to symbols.
  #
  # See:
  # - [original code](https://avdi.codes/recursively-symbolize-keys/)
  # - [array support](https://gist.github.com/neektza/8585746)
  #
  # @param arg [Hash,Array]
  # @return [Hash,Array]
  def self.symbolize_keys(arg)
    if arg.is_a?(Hash)
      arg.inject({}) do |result, (key, value)|
        new_key = case key
                  when String then key.to_sym()
                  else key
                  end
        new_value = case value
                    when Hash then symbolize_keys(value)
                    when Array then symbolize_keys(value)
                    else value
                    end
        result[new_key] = new_value
        result
      end
    elsif arg.is_a?(Array)
      arg.map { |e| symbolize_keys(e) }
    else
      arg
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
coolkit-0.2.2 lib/coolkit/symbolize.rb
coolkit-0.2.1 lib/coolkit/symbolize.rb