diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-01-11 22:06:05 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-01-11 22:06:05 +0000 |
commit | 1497522ab0b84f39a04774b4a6a86dea32aa9a3e (patch) | |
tree | 86a8908581bb23153c20f6c3026a0e0ecb95a405 /activesupport/lib/core_ext/hash/keys.rb | |
parent | d44deadd3912ee44d12edfc4579ed58323e4c393 (diff) | |
download | rails-1497522ab0b84f39a04774b4a6a86dea32aa9a3e.tar.gz rails-1497522ab0b84f39a04774b4a6a86dea32aa9a3e.tar.bz2 rails-1497522ab0b84f39a04774b4a6a86dea32aa9a3e.zip |
Fixed the half applied 393 changeset
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@395 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/core_ext/hash/keys.rb')
-rw-r--r-- | activesupport/lib/core_ext/hash/keys.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/activesupport/lib/core_ext/hash/keys.rb b/activesupport/lib/core_ext/hash/keys.rb new file mode 100644 index 0000000000..0db37a6374 --- /dev/null +++ b/activesupport/lib/core_ext/hash/keys.rb @@ -0,0 +1,36 @@ +module ActiveSupport + module CoreExtensions + module Hash + module Keys + + # Return a new hash with all keys converted to symbols. + def symbolize_keys + inject({}) do |options, (key, value)| + options[key.to_sym] = value + options + end + end + + # Destructively convert all keys to symbols. + def symbolize_keys! + keys.each do |key| + unless key.is_a?(Symbol) + self[key.to_sym] = self[key] + delete(key) + end + end + self + end + + alias_method :to_options, :symbolize_keys + alias_method :to_options!, :symbolize_keys! + + def assert_valid_keys(valid_keys) + unknown_keys = keys - valid_keys + raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty? + end + + end + end + end +end |