Skip to content
This repository
Browse code

Hash#fetch(fetch) is not the same as doing hash[key]

  • Loading branch information...
commit 98f4aee8dac22d9e9bb3c122b43e9e5ee8ba7d1c 1 parent 3561e85
Xavier Noria authored July 21, 2012
12  activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -96,7 +96,17 @@ def key?(key)
96 96
     alias_method :has_key?, :key?
97 97
     alias_method :member?, :key?
98 98
 
99  
-    # Fetches the value for the specified key, same as doing hash[key]
  99
+    # Same as <tt>Hash#fetch</tt> where the key passed as argument can be
  100
+    # either a string or a symbol:
  101
+    #
  102
+    #   counters = HashWithIndifferentAccess.new
  103
+    #   counters[:foo] = 1
  104
+    #
  105
+    #   counters.fetch("foo")          # => 1
  106
+    #   counters.fetch(:bar, 0)        # => 0
  107
+    #   counters.fetch(:bar) {|key| 0} # => 0
  108
+    #   counters.fetch(:zoo)           # => KeyError: key not found: "zoo"
  109
+    #
100 110
     def fetch(key, *extras)
101 111
       super(convert_key(key), *extras)
102 112
     end

0 notes on commit 98f4aee

Please sign in to comment.
Something went wrong with that request. Please try again.