提交 fc6a5253 编写于 作者: S Stefan Schüßler

Add HashWithIndifferentAccess#assoc

上级 deac9ec4
* Add `ActiveSupport::HashWithIndifferentAccess#assoc`.
`assoc` can now be called with either a string or a symbol.
*Stefan Schüßler*
## Rails 6.0.0.beta1 (January 18, 2019) ##
* Remove deprecated `Module#reachable?` method.
......
......@@ -164,6 +164,19 @@ def [](key)
super(convert_key(key))
end
# Same as <tt>Hash#assoc</tt> where the key passed as argument can be
# either a string or a symbol:
#
# counters = ActiveSupport::HashWithIndifferentAccess.new
# counters[:foo] = 1
#
# counters.assoc('foo') # => ["foo", 1]
# counters.assoc(:foo) # => ["foo", 1]
# counters.assoc(:zoo) # => nil
def assoc(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:
#
......
......@@ -447,6 +447,14 @@ def test_indifferent_transform_values_bang
assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
end
def test_indifferent_assoc
indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@strings)
key, value = indifferent_strings.assoc(:a)
assert_equal("a", key)
assert_equal(1, value)
end
def test_indifferent_compact
hash_contain_nil_value = @strings.merge("z" => nil)
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_contain_nil_value)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册