HashWithIndifferentAccess better respects #to_hash (4-1-stable backport)

Backports the stable parts of #12550.