diff options
Diffstat (limited to 'activesupport/lib/active_support/hash_with_indifferent_access.rb')
-rw-r--r-- | activesupport/lib/active_support/hash_with_indifferent_access.rb | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 71713644a7..0c78f1611f 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -1,7 +1,8 @@ require 'active_support/core_ext/hash/keys' module ActiveSupport - # Implements a hash where keys <tt>:foo</tt> and <tt>"foo"</tt> are considered to be the same. + # Implements a hash where keys <tt>:foo</tt> and <tt>"foo"</tt> are considered + # to be the same. # # rgb = ActiveSupport::HashWithIndifferentAccess.new # @@ -15,17 +16,17 @@ module ActiveSupport # # Internally symbols are mapped to strings when used as keys in the entire # writing interface (calling <tt>[]=</tt>, <tt>merge</tt>, etc). This - # mapping belongs to the public interface. For example, given + # mapping belongs to the public interface. For example, given: # - # hash = ActiveSupport::HashWithIndifferentAccess.new(:a => 1) + # hash = ActiveSupport::HashWithIndifferentAccess.new(a: 1) # - # you are guaranteed that the key is returned as a string: + # You are guaranteed that the key is returned as a string: # # hash.keys # => ["a"] # # Technically other types of keys are accepted: # - # hash = ActiveSupport::HashWithIndifferentAccess.new(:a => 1) + # hash = ActiveSupport::HashWithIndifferentAccess.new(a: 1) # hash[0] = 0 # hash # => {"a"=>1, 0=>0} # @@ -35,11 +36,11 @@ module ActiveSupport # # Note that core extensions define <tt>Hash#with_indifferent_access</tt>: # - # rgb = {:black => '#000000', :white => '#FFFFFF'}.with_indifferent_access + # rgb = { black: '#000000', white: '#FFFFFF' }.with_indifferent_access # # which may be handy. class HashWithIndifferentAccess < Hash - # Returns true so that <tt>Array#extract_options!</tt> finds members of + # Returns +true+ so that <tt>Array#extract_options!</tt> finds members of # this class. def extractable_options? true @@ -86,9 +87,9 @@ module ActiveSupport # Assigns a new value to the hash: # # hash = ActiveSupport::HashWithIndifferentAccess.new - # hash[:key] = "value" + # hash[:key] = 'value' # - # This value can be later fetched using either +:key+ or +"key"+. + # This value can be later fetched using either +:key+ or +'key'+. def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end @@ -98,10 +99,10 @@ module ActiveSupport # Updates the receiver in-place, merging in the hash passed as argument: # # hash_1 = ActiveSupport::HashWithIndifferentAccess.new - # hash_1[:key] = "value" + # hash_1[:key] = 'value' # # hash_2 = ActiveSupport::HashWithIndifferentAccess.new - # hash_2[:key] = "New Value!" + # hash_2[:key] = 'New Value!' # # hash_1.update(hash_2) # => {"key"=>"New Value!"} # @@ -120,7 +121,6 @@ module ActiveSupport # hash_1[:key] = 10 # hash_2['key'] = 12 # hash_1.update(hash_2) { |key, old, new| old + new } # => {"key"=>22} - # def update(other_hash) if other_hash.is_a? HashWithIndifferentAccess super(other_hash) @@ -140,10 +140,9 @@ module ActiveSupport # Checks the hash for a key matching the argument passed in: # # hash = ActiveSupport::HashWithIndifferentAccess.new - # hash["key"] = "value" + # hash['key'] = 'value' # hash.key?(:key) # => true - # hash.key?("key") # => true - # + # hash.key?('key') # => true def key?(key) super(convert_key(key)) end @@ -158,11 +157,10 @@ module ActiveSupport # counters = ActiveSupport::HashWithIndifferentAccess.new # counters[:foo] = 1 # - # counters.fetch("foo") # => 1 + # counters.fetch('foo') # => 1 # counters.fetch(:bar, 0) # => 0 # counters.fetch(:bar) {|key| 0} # => 0 # counters.fetch(:zoo) # => KeyError: key not found: "zoo" - # def fetch(key, *extras) super(convert_key(key), *extras) end @@ -170,10 +168,9 @@ module ActiveSupport # Returns an array of the values at the specified indices: # # hash = ActiveSupport::HashWithIndifferentAccess.new - # hash[:a] = "x" - # hash[:b] = "y" - # hash.values_at("a", "b") # => ["x", "y"] - # + # hash[:a] = 'x' + # hash[:b] = 'y' + # hash.values_at('a', 'b') # => ["x", "y"] def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end @@ -197,8 +194,7 @@ module ActiveSupport # # hash = ActiveSupport::HashWithIndifferentAccess.new # hash['a'] = nil - # hash.reverse_merge(:a => 0, :b => 1) # => {"a"=>nil, "b"=>1} - # + # hash.reverse_merge(a: 0, b: 1) # => {"a"=>nil, "b"=>1} def reverse_merge(other_hash) super(self.class.new_from_hash_copying_default(other_hash)) end |