diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-02-12 20:10:10 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-02-12 20:10:10 +0000 |
commit | 83c3f6f81a4dbb618518aac9e411cd1f675fe571 (patch) | |
tree | e0cfc508a0fdea21116f4bfb319f3f43948ab5e3 /activesupport/lib | |
parent | 61a6a440cb8bc4694828c437fb1996db270c9ee6 (diff) | |
download | rails-83c3f6f81a4dbb618518aac9e411cd1f675fe571.tar.gz rails-83c3f6f81a4dbb618518aac9e411cd1f675fe571.tar.bz2 rails-83c3f6f81a4dbb618518aac9e411cd1f675fe571.zip |
Added Hash#stringify_keys and Hash#stringify_keys!
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@586 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/core_ext/hash/keys.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/activesupport/lib/core_ext/hash/keys.rb b/activesupport/lib/core_ext/hash/keys.rb index 4dd982337c..3c301c6fa6 100644 --- a/activesupport/lib/core_ext/hash/keys.rb +++ b/activesupport/lib/core_ext/hash/keys.rb @@ -2,6 +2,25 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Hash #:nodoc: module Keys + # Return a new hash with all keys converted to strings. + def stringify_keys + inject({}) do |options, (key, value)| + options[key.to_s] = value + options + end + end + + # Destructively convert all keys to strings. + def stringify_keys! + keys.each do |key| + unless key.is_a?(String) + self[key.to_s] = self[key] + delete(key) + end + end + self + end + # Return a new hash with all keys converted to symbols. def symbolize_keys inject({}) do |options, (key, value)| |