Created
April 14, 2011 20:17
-
-
Save jtrim/920392 to your computer and use it in GitHub Desktop.
Methods to help turn all hash keys into symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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