What would be the output of this code snippet?
Hint: note the type of the key in the hash and the type of the key when accessing the hash.
The output is
nil because the hash is defined using symbols, and the access to it was done using a string.
Rails’ active_support introduced a class called
HashWithIndifferentAccess (a subclass of
Hash), which, according to the class’ doc:
[…] we only have it so that people can write params[:key] instead of params[‘key’] and they get the same value for both keys.
Referring back to the example above, we could get what we want (indifference between symbol or string hash access) with:
which would print