Rails test for hash type

hash, ruby-on-rails

Solution

From the docs on ActiveSupport::HashWithIndifferentAccess:

This class has dubious semantics and we only have it so that people can write params[:key] instead of params[‘key’] and they get the same value for both keys.

So, it's a class that inherits from Hash to allow you to pass a symbol or a string as the key and return the same value for either.

To fix (and clean up) your tests, you could just use the following:

some_object.is_a? Hash

This will return true if it's a Hash or a descendant of Hash.

Problem

In Sinatra, I could test for some_object.class.name == "Hash". Now, after a submit, I have to test for that, plus == "ActiveSupport::HashWithIndifferentAccess", for my code to work. Why is that, and do I have to update all the places where that comparison happens, or is there an easier way? thanks

Original source