Permalink
Browse files

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 01ae39660243bc5f0a986e20f9c9bff312b1b5f8
Showing with 5 additions and 1 deletion.
  1. +4 −0 activesupport/CHANGELOG.md
  2. +1 −1 activesupport/lib/active_support/core_ext/hash/slice.rb
@@ -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,
@@ -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.

2 comments on commit 01ae396

@rafaelfranca

This comment has been minimized.

Member

rafaelfranca replied Oct 26, 2017

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

This comment has been minimized.

Member

amatsuda replied Oct 27, 2017

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

Please sign in to comment.