Skip to content

Instantly share code, notes, and snippets.

@jtrim
Created April 14, 2011 20:17
Show Gist options
  • Select an option

  • Save jtrim/920392 to your computer and use it in GitHub Desktop.

Select an option

Save jtrim/920392 to your computer and use it in GitHub Desktop.
Methods to help turn all hash keys into symbols
class Hash
def symbolize_keys
replace( keys.inject({}) { |new_hash,key| new_hash[key.to_sym] = self[key]; new_hash } )
end
def symbolize_keys!
symbolized_hash = keys.inject({}) do |new_hash,key|
new_hash[key.to_sym] = self[key]
new_hash[key.to_sym].symbolize_keys! if new_hash[key.to_sym].respond_to? :symbolize_keys!
new_hash
end
self.replace( symbolized_hash )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment