Sha256: a89e44aee62463baee16df13281dd52d8a745a6fd0cae536cc5f2e3bd1ab476f

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

module Hashie
  module Extensions
    module StringifyKeys
      # Convert all keys in the hash to strings.
      #
      # @example
      #   test = {:abc => 'def'}
      #   test.stringify_keys!
      #   test # => {'abc' => 'def'}
      def stringify_keys!
        keys.each do |k|
          stringify_keys_recursively!(self[k])
          self[k.to_s] = delete(k)
        end
        self
      end

      # Return a new hash with all keys converted
      # to strings.
      def stringify_keys
        dup.stringify_keys!
      end

      protected

      # Stringify all keys recursively within nested
      # hashes and arrays.
      def stringify_keys_recursively!(object)
        if self.class === object
          object.stringify_keys!
        elsif ::Array === object
          object.each do |i|
            stringify_keys_recursively!(i)
          end
          object
        elsif object.respond_to?(:stringify_keys!)
          object.stringify_keys!
        elsif ::Hash === object
          object.keys.each do |k|
            stringify_keys_recursively!(object[k])
            object[k.to_s] = object.delete(k)
          end
          object
        else
          object
        end
      end
    end
  end
end

Version data entries

5 entries across 4 versions & 2 rubygems

Version Path
hashie-3.3.2 lib/hashie/extensions/stringify_keys.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/hashie-3.3.1/lib/hashie/extensions/stringify_keys.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/hashie-3.3.1/lib/hashie/extensions/stringify_keys.rb
hashie-3.3.1 lib/hashie/extensions/stringify_keys.rb
hashie-3.2.0 lib/hashie/extensions/stringify_keys.rb