Always double check symbol vs string keys in hash objects

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the ruby category.

Last Updated: 2021-05-16

I had something like the following to store my config for a Redis DB instance:

  url: redis://localhost:6379
  db: 2
  url: redis://localhost:6379
  db: 3

This config was consumed with this code:

config_file = Rails.root.join('config', 'redis.yml')
config = YAML.safe_load([Rails.env] || {}

Redis.current = config[:url], db: config[:db])

The issue was that, despite the symbol like presentation of the yaml keys in the yaml file, it was in fact parsed as strings by Ruby. Therefore my reference of config[:url] contained nil and my redis connection failed.

Here is the fix:

config_file = Rails.root.join('config', 'redis.yml')
Redis.current = config["url"], db: config["db"])


Always check whether some hash or hash-like object actually contains symbols or strings -- especially if it is generated.