aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2012-07-21 08:04:37 +0200
committerXavier Noria <fxn@hashref.com>2012-07-21 08:04:37 +0200
commit98f4aee8dac22d9e9bb3c122b43e9e5ee8ba7d1c (patch)
tree51ec469c7b68c20ff793bf6b98401bc0b1ebc999
parent3561e85bb18cb0b5fed275c8494065786821c05e (diff)
downloadrails-98f4aee8dac22d9e9bb3c122b43e9e5ee8ba7d1c.tar.gz
rails-98f4aee8dac22d9e9bb3c122b43e9e5ee8ba7d1c.tar.bz2
rails-98f4aee8dac22d9e9bb3c122b43e9e5ee8ba7d1c.zip
Hash#fetch(fetch) is not the same as doing hash[key]
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb12
1 files changed, 11 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb
index 3e6c8893e9..bb559f4b4a 100644
--- a/activesupport/lib/active_support/hash_with_indifferent_access.rb
+++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -96,7 +96,17 @@ module ActiveSupport
alias_method :has_key?, :key?
alias_method :member?, :key?
- # Fetches the value for the specified key, same as doing hash[key]
+ # Same as <tt>Hash#fetch</tt> where the key passed as argument can be
+ # either a string or a symbol:
+ #
+ # counters = HashWithIndifferentAccess.new
+ # counters[:foo] = 1
+ #
+ # counters.fetch("foo") # => 1
+ # counters.fetch(:bar, 0) # => 0
+ # counters.fetch(:bar) {|key| 0} # => 0
+ # counters.fetch(:zoo) # => KeyError: key not found: "zoo"
+ #
def fetch(key, *extras)
super(convert_key(key), *extras)
end