-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restore Hash#transform_keys behavior to always return a Hash instance #24517
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @eileencodes (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
I wonder if returning the actual class type makes more sense indeed. A solution for Mongoid, would be to stop extending Hash object. Thats a rule of thumb I learned in ruby, and try to avoid as much as possible. cc @tenderlove which loves extending core ruby classes . |
Although I submitted this change to always return a Hash from the transform_keys method, I do think returning an instance of self.class makes more sense. Returning a Hash, regardless of what self is, seems unexpected to me. That said, this pull request was intended to restore previous behavior and was originally opened against 4.2-stable. |
This is how |
Agreed, we should match Ruby's behavior on other relevant methods. Subclasses can override this if need be (or not subclass Hash!) |
Thanks for your comments. I agree that matching Ruby's behavior is best. |
… was fixed in Mongoid 5.0 See: rails/rails#24517
… was fixed in Mongoid 5.0 (#5544) See: rails/rails#24517
Summary
A failure in mongoid's test suite alerted me to a behavior difference between activesupport 4.1 and 4.2:
Hash#symbolize_keys
returns aHash
.Hash#symbolize_keys
returns an instance ofself.class
.Here is the related commit.
This behavior change is significant when
self
inherits fromHash
. For example, aBSON::Document
(defined in the bson gem, on which mongoid depends) inherits fromHash
: