aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/hash/keys.rb
diff options
context:
space:
mode:
authorthedarkone <thedarkone2@gmail.com>2010-09-24 23:21:59 +0200
committerthedarkone <thedarkone2@gmail.com>2010-09-27 17:45:59 +0200
commit77efc20a54708ba37ba679ffe90021bf8a8d3a8a (patch)
tree501f8c1c8d09327cd89408dc751140493195540e /activesupport/lib/active_support/core_ext/hash/keys.rb
parent4c360c15e53324a7e5940f943129a06da51ac7d3 (diff)
downloadrails-77efc20a54708ba37ba679ffe90021bf8a8d3a8a.tar.gz
rails-77efc20a54708ba37ba679ffe90021bf8a8d3a8a.tar.bz2
rails-77efc20a54708ba37ba679ffe90021bf8a8d3a8a.zip
Make assert_valid_keys slightly faster.
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash/keys.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb10
1 files changed, 6 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb
index 2cb2c89d30..d8748b1138 100644
--- a/activesupport/lib/active_support/core_ext/hash/keys.rb
+++ b/activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -35,11 +35,13 @@ class Hash
# as keys, this will fail.
#
# ==== Examples
- # { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
- # { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
+ # { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: years"
+ # { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key: name"
# { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
def assert_valid_keys(*valid_keys)
- unknown_keys = keys - valid_keys.flatten
- raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
+ valid_keys.flatten!
+ each_key do |k|
+ raise(ArgumentError, "Unknown key: #{k}") unless valid_keys.include?(k)
+ end
end
end