Skip to content

Commit

Permalink
Hash#fetch(fetch) is not the same as doing hash[key]
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Jul 21, 2012
1 parent 3561e85 commit 98f4aee
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion activesupport/lib/active_support/hash_with_indifferent_access.rb
Expand Up @@ -96,7 +96,17 @@ def key?(key)
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
Expand Down

0 comments on commit 98f4aee

Please sign in to comment.