From fc6a525341d7a287740d3e61beaccabd3e65bdc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Sch=C3=BC=C3=9Fler?= Date: Tue, 29 Jan 2019 11:49:23 +0100 Subject: Add HashWithIndifferentAccess#assoc --- .../lib/active_support/hash_with_indifferent_access.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index f1af76019a..3a2b2652c4 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -164,6 +164,19 @@ module ActiveSupport super(convert_key(key)) end + # Same as Hash#assoc 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 Hash#fetch where the key passed as argument can be # either a string or a symbol: # -- cgit v1.2.3