Type:
Class

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

1
2
3
4
5
6
7
8
9
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:

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

You are guaranteed that the key is returned as a string:

1
hash.keys # => ["a"]

Technically other types of keys are accepted:

1
2
3
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:

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

which may be handy.

new
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

new(constructor = {}) Class Public methods

2025-01-10 15:47:30
deep_symbolize_keys
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

deep_symbolize_keys() Instance Public methods

2025-01-10 15:47:30
symbolize_keys
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

symbolize_keys() Instance Public methods

2025-01-10 15:47:30
regular_writer
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

regular_writer(key, value) Instance Public methods Alias for:

2025-01-10 15:47:30
has_key?
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

has_key?(key) Instance Public methods Alias for:

2025-01-10 15:47:30
fetch
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

fetch(key, *extras) Instance Public methods Same as Hash#fetch

2025-01-10 15:47:30
stringify_keys!
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

stringify_keys!() Instance Public methods

2025-01-10 15:47:30
deep_stringify_keys
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

deep_stringify_keys() Instance Public methods

2025-01-10 15:47:30
update
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

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

2025-01-10 15:47:30
select
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/Object::HashWithIndifferentAccess

select(*args, &block) Instance Public methods

2025-01-10 15:47:30