Rails test for hash type

In Sinatra, I can check for some_object.class.name == "Hash". Now, after submitting, I have to check for this, plus == "ActiveSupport :: HashWithIndifferentAccess" for my code to work. Why is this so, and do I need to update all the places where the comparison takes place, or is there an easier way? thanks

+6
source share
1 answer

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, this is a class that inherits from Hash so you can pass a character or string as a key and return the same value for.

To fix (and clean) your tests, you can simply use the following:

some_object.is_a? Hash 

This will return true if it is a hash or a descendant of a hash.

+15
source

Source: https://habr.com/ru/post/918610/


All Articles