From 83c3f6f81a4dbb618518aac9e411cd1f675fe571 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 12 Feb 2005 20:10:10 +0000 Subject: Added Hash#stringify_keys and Hash#stringify_keys! git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@586 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/core_ext/hash/keys.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'activesupport/lib/core_ext') 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)| -- cgit v1.2.3