提交 38a99023 编写于 作者: D David Cornu

Match HashWithIndifferentAccess#default's behaviour with Hash#default

上级 b4f28dbb
* Match `HashWithIndifferentAccess#default`'s behaviour with `Hash#default`
*David Cornu*
* Adds `:exception_object` key to `ActiveSupport::Notifications::Instrumenter` payload when an exception is raised.
Adds new key/value pair to payload when an exception is raised: e.g. `:exception_object => #<RuntimeError: FAIL>`.
......
......@@ -68,12 +68,10 @@ def initialize(constructor = {})
end
end
def default(key = nil)
if key.is_a?(Symbol) && include?(key = key.to_s)
self[key]
else
super
end
def default(*args)
key = args.first
args[0] = key.to_s if key.is_a?(Symbol)
super(*args)
end
def self.new_from_hash_copying_default(hash)
......@@ -159,6 +157,20 @@ def key?(key)
alias_method :has_key?, :key?
alias_method :member?, :key?
# Same as <tt>Hash#[]</tt> where the key passed as argument can be
# either a string or a symbol:
#
# counters = ActiveSupport::HashWithIndifferentAccess.new
# counters[:foo] = 1
#
# counters['foo'] # => 1
# counters[:foo] # => 1
# counters[:zoo] # => nil
def [](key)
super(convert_key(key))
end
# Same as <tt>Hash#fetch</tt> where the key passed as argument can be
# either a string or a symbol:
#
......
......@@ -1587,9 +1587,9 @@ def test_should_use_default_proc_for_unknown_key
assert_equal 3, hash_wia[:new_key]
end
def test_should_use_default_proc_if_no_key_is_supplied
def test_should_return_nil_if_no_key_is_supplied
hash_wia = HashWithIndifferentAccess.new { 1 + 2 }
assert_equal 3, hash_wia.default
assert_equal nil, hash_wia.default
end
def test_should_use_default_value_for_unknown_key
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册