diff options
author | David Cornu <davidjcornu@gmail.com> | 2015-02-11 21:49:09 +0000 |
---|---|---|
committer | David Cornu <davidjcornu@gmail.com> | 2016-01-07 15:39:36 -0500 |
commit | 38a990237a0f99d99b885ea6570cae3752138a29 (patch) | |
tree | c4c4d5fe1bff0b326392a530571d4488721fa7d9 /activesupport/lib | |
parent | b4f28dbb39f29fcb0b0393b26ef31f1f6fbe0679 (diff) | |
download | rails-38a990237a0f99d99b885ea6570cae3752138a29.tar.gz rails-38a990237a0f99d99b885ea6570cae3752138a29.tar.bz2 rails-38a990237a0f99d99b885ea6570cae3752138a29.zip |
Match HashWithIndifferentAccess#default's behaviour with Hash#default
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/hash_with_indifferent_access.rb | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 4ff35a45a1..b878f31e75 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -68,12 +68,10 @@ module ActiveSupport 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 @@ module ActiveSupport 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: # |