Maintain subclasses of Hash when calling Hash#slice #250

wants to merge 1 commit into


None yet

2 participants


This patch corrects i18n's implementation of Hash#slice so that it always returns an object of the same class it was originally called on.

In the current implementation, it creates a new hash, copies the selected key/value pairs into it, and returns it. This causes the returned value to be a different class, in the case where the original object was a subclass of Hash. This behavior is problematic when using something like Hashie or HashWithIndifferentAccess, because translation string interpolation only works for symbols, and keys can be copied into the new hash as strings, depending on the implementation of the subclass.

I discovered this in another library that returns Hashie::Rash objects, which work like allow symbol and string key access, but when copied by i18n's Hash#slice, lose their ability to look up values by symbol keys.


@jimmycuadra is this library you comment not using Rails at all? Because if it is, it should be picking Rails' version over I18n I believe.

Anyway, people relying on I18n's core extensions make me really sad. Maybe I'll deprecate this on the future.



The library I was referring to is rmoriz/digital_ocean, which does not involve Rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment