I was recently going through a PR and realised we were creating new hash in 2 ways across our codebase.
hash = HashWithIndifferentAccess.new
hash = {}
It just occured to me I never read up on which way is better or why we do this. So here's a small dive into this.
HashWithIndifferentAccess(HWIA)
Class HashWithIndifferentAccess is inherited from ruby "Hash" & above special behavior is added in it. As the name suggests you can access keys in either ways, string or symbol.
Basically hash[:key]
and hash['key']
are same in HWIA case but different for ruby hashes.
Which is better?
Using symbols not only saves time when doing comparisons, but also saves memory, because they are only stored once. It's the ruby way of doing things. But in most cases it won't make much of a diffrence unless you are working with really large Hashes.
Here is an article that goes into benchmarking strings vs symbols
for those interested.