Sha256: c861fb4f3b4947c1ee26e02ea5f3cd9053d1a928374fd9a4daff6a30f0702ef5

Contents?: true

Size: 635 Bytes

Versions: 2

Compression:

Stored size: 635 Bytes

Contents

module Incollege
  module HashHelper
    # Return a new hash with all keys converted to symbols, as long as
    # they respond to +to_sym+.
    #
    #   { 'name' => 'Rob', 'years' => '28' }.symbolize_keys
    #   #=> { :name => "Rob", :years => "28" }
    def self.symbolize_keys(hash)
      symbolize_keys!(hash.dup)
    end

    # Destructively convert all keys to symbols, as long as they respond
    # to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
    def self.symbolize_keys!(hash)
      hash.keys.each do |key|
        hash[(key.to_sym rescue key) || key] = hash.delete(key)
      end
      hash
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
incollege-text-1.12.1 lib/incollege-text/hash_helper.rb
incollege-text-1.12.0 lib/incollege-text/hash_helper.rb