mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add HashWithIndifferentAccess#assoc
This commit is contained in:
parent
deac9ec430
commit
fc6a525341
3 changed files with 27 additions and 0 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
* 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) ##
|
## Rails 6.0.0.beta1 (January 18, 2019) ##
|
||||||
|
|
||||||
* Remove deprecated `Module#reachable?` method.
|
* Remove deprecated `Module#reachable?` method.
|
||||||
|
|
|
@ -164,6 +164,19 @@ module ActiveSupport
|
||||||
super(convert_key(key))
|
super(convert_key(key))
|
||||||
end
|
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
|
# Same as <tt>Hash#fetch</tt> where the key passed as argument can be
|
||||||
# either a string or a symbol:
|
# either a string or a symbol:
|
||||||
#
|
#
|
||||||
|
|
|
@ -447,6 +447,14 @@ class HashWithIndifferentAccessTest < ActiveSupport::TestCase
|
||||||
assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
|
assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
|
||||||
end
|
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
|
def test_indifferent_compact
|
||||||
hash_contain_nil_value = @strings.merge("z" => nil)
|
hash_contain_nil_value = @strings.merge("z" => nil)
|
||||||
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_contain_nil_value)
|
hash = ActiveSupport::HashWithIndifferentAccess.new(hash_contain_nil_value)
|
||||||
|
|
Loading…
Reference in a new issue