# How to Access a Ruby Hash with both String and Symbol Keys

*Published on September 27, 2023*

Tags: Rails Internals

---


If you try to access a regular symbolized Hash in Ruby with a string key, it returns&nbsp;`nil`&nbsp;value.

```ruby
framework = { name: 'Ruby on Rails', language: 'Ruby' }

framework[:name]       # "Ruby on Rails"
framework['language']  # nil
```

To design a better method API, you may want to allow the users (or method callers) to pass any key, without worrying whether it has to be a string or a symbol.

To access the Hash without worrying about the type of the key, use the&nbsp;`ActiveSupport::HashWithIndifferentAccess`&nbsp;class provided in ActiveSupport library. It implements a hash where keys&nbsp;`:name`&nbsp;and&nbsp;`"name"`&nbsp;are considered to be the same.

```ruby
require "active_support/hash_with_indifferent_access"

framework = ActiveSupport::HashWithIndifferentAccess.new
framework[:name] = 'Ruby on Rails'

puts framework[:name]   # Ruby on Rails
puts framework['name']  # Ruby on Rails
```

You can also pass an existing Hash to the constructor to get a new instance:

```ruby
require "active_support/hash_with_indifferent_access"

framework = { name: 'Ruby on Rails', language: 'Ruby' }
result = ActiveSupport::HashWithIndifferentAccess.new(framework)

puts result['name']      # Ruby on Rails
puts result[:language]   # Ruby
```

If that long class name looks intimidating, don't worry, you don't have to initialize this class yourself. Instead, use the&nbsp;`with_indifferent_access`&nbsp;extension method.

```ruby
require "active_support/core_ext/hash/indifferent_access"

framework = { name: 'Ruby on Rails', language: 'Ruby' }
result = framework.with_indifferent_access

puts result[:name]       # Ruby on Rails
puts result['language']  # Ruby
```

The&nbsp;`with_indifferent_access`&nbsp;method is added by Active Support by monkey-patching the&nbsp;`Hash`&nbsp;class. It returns a new instance of the&nbsp;`ActiveSupport::HashWithIndifferentAccess`, initializing it with the current hash.

```ruby
# lib/active_support/core_ext/hash/indifferent_access.rb

class Hash
  def with_indifferent_access
    ActiveSupport::HashWithIndifferentAccess.new(self)
  end
end
```

This class supports all the existing hash API, so you can call any method you'd call on a regular Ruby hash.

A good real-world example is the&nbsp;`params`&nbsp;hash in Rails controllers, which can be accessed with either a string or a symbol.

```ruby
params = ActionController::Parameters.new(name: "Ruby")

params[:name]  # Ruby
params['name'] # Ruby
```

Behind the scenes, it uses the&nbsp;`with_indifferent_access`&nbsp;method as follows:

```ruby
module ActionController
  class Parameters
    def initialize(parameters = {}, logging_context = {})
      # ...
      
      @parameters = parameters.with_indifferent_access
    end
  end
end
```

**What about the performance?**

If you're worried about performance, check out this Reddit post for a nuanced discussion:&nbsp;[Why wouldn't you want a hash with indifferent access?](https://www.reddit.com/r/ruby/comments/13q4txp/why_wouldnt_you_want_a_hash_with_indifferent/)

However, as the user&nbsp;_stillness\_still_&nbsp;mentions:

> I got 99 problems and the lookup time difference between a symbol and a string ain't one.

Well said, sir, well said.

