Type:
Class

Implements a hash where keys :foo and "foo" are considered to be the same.

rgb = ActiveSupport::HashWithIndifferentAccess.new

rgb[:black] = '#000000'
rgb[:black]  # => '#000000'
rgb['black'] # => '#000000'

rgb['white'] = '#FFFFFF'
rgb[:white]  # => '#FFFFFF'
rgb['white'] # => '#FFFFFF'

Internally symbols are mapped to strings when used as keys in the entire writing interface (calling []=, merge, etc). This mapping belongs to the public interface. For example, given:

hash = ActiveSupport::HashWithIndifferentAccess.new(a: 1)

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[0] = 0
hash # => {"a"=>1, 0=>0}

but this class is intended for use cases where strings or symbols are the expected keys and it is convenient to understand both as the same. For example the params hash in Ruby on Rails.

Note that core extensions define Hash#with_indifferent_access:

rgb = { black: '#000000', white: '#FFFFFF' }.with_indifferent_access

which may be handy.

replace

replace(other_hash) Instance Public methods Replaces the contents of this hash

2015-06-20 00:00:00
with_indifferent_access

with_indifferent_access() Instance Public methods

2015-06-20 00:00:00
key?

key?(key) Instance Public methods Checks the hash for a key matching the argument

2015-06-20 00:00:00
update

update(other_hash) Instance Public methods Updates the receiver in-place, merging

2015-06-20 00:00:00
member?

member?(key) Instance Public methods Alias for:

2015-06-20 00:00:00
stringify_keys

stringify_keys() Instance Public methods

2015-06-20 00:00:00
regular_update

regular_update(other_hash) Instance Public methods Alias for:

2015-06-20 00:00:00
nested_under_indifferent_access

nested_under_indifferent_access() Instance Public methods

2015-06-20 00:00:00
new_from_hash_copying_default

new_from_hash_copying_default(hash) Class Public methods

2015-06-20 00:00:00
to_hash

to_hash() Instance Public methods Convert to a regular hash with string keys

2015-06-20 00:00:00