module CoreExtensions::Hash::Keys
Public Instance Methods
rename_keys(mapping)
click to toggle source
# File lib/core_extensions/hash/keys.rb, line 9 def rename_keys(mapping) result = {} self.each do |k, v| mapped_key = mapping[k] ? mapping[k] : k # If this entry is a deep/nested hash, recursively change key names. result[mapped_key] = v.kind_of?(::Hash) ? v.rename_keys(mapping) : v result[mapped_key] = v.collect{ |obj| obj.rename_keys(mapping) if obj.kind_of?(::Hash) } if v.kind_of?(::Array) end # If used within a Rails project, allow reference by either symbol or string. result.respond_to?(:with_indifferent_access) ? result.with_indifferent_access : result end