Sha256: 6f2fc152e1680afde9103be2f12576e3f009cf112f35c30c14bdbc09e271a2fc

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module SimplyUseful
  module CoreExtensions
    module Keys

      # How it's done in active support
      # File lib/active_support/core_ext/hash/keys.rb, line 22
      #22:         def symbolize_keys
      #23:           inject({}) do |options, (key, value)|
      #24:             options[(key.to_sym rescue key) || key] = value
      #25:             options
      #26:           end
      #27:         end

      # Another article mentioning that method
      # http://www.any-where.de/blog/ruby-hash-convert-string-keys-to-symbols/

      def deep_symbolize_keys
        _deep_symbolize_keys(self)
      end

      def deep_symbolize_keys!
        self.replace(self.deep_symbolize_keys)
      end

      private

      def _deep_symbolize_keys(value)
        return value unless value.is_a?(Hash)
        hash = value.inject({}) do |memo,(k,v)|
          memo[(k.to_sym rescue k)|| k] = _deep_symbolize_keys(v)
          memo
        end
        return hash
      end

    end
  end
end

class Hash
  include SimplyUseful::CoreExtensions::Keys
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simply_useful-0.1.5 lib/core_ext/hash.rb