Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Let Hash#slice return a Hash
In order to keep this method compatible with the Ruby 2.5 version of Hash#slice.
This bahavior is actually slightly incompatibile with previous versions of Active Support
but it might not cause a real problem, since HWIA, the biggest use case of Hash subclassing here,
already overrides `slice` to return another HWIA.
  • Loading branch information
amatsuda committed Oct 21, 2017
1 parent d6f3b91 commit 01ae396
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,7 @@
* `Hash#slice` now falls back to Ruby 2.5+'s built-in definition if defined.

*Akira Matsuda*

* Deprecate `secrets.secret_token`.

The architecture for secrets had a big upgrade between Rails 3 and Rails 4,
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/core_ext/hash/slice.rb
Expand Up @@ -21,7 +21,7 @@ class Hash
# valid_keys = [:mass, :velocity, :time]
# search(options.slice(*valid_keys))
def slice(*keys)
keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
keys.each_with_object(Hash.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
end unless method_defined?(:slice)

# Replaces the hash with only the given keys.
Expand Down

2 comments on commit 01ae396

@rafaelfranca
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to change i18n because it does not return an instance of Hash. See https://travis-ci.org/svenfuchs/i18n/jobs/293285567, that now is failing because of this commit. Mind to open a PR?

@amatsuda
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca Ah, thank you for letting me know this! Yes, I'll work on this soon!

Please sign in to comment.