From 01ae39660243bc5f0a986e20f9c9bff312b1b5f8 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 21 Oct 2017 17:24:18 +0900 Subject: 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. --- activesupport/CHANGELOG.md | 4 ++++ activesupport/lib/active_support/core_ext/hash/slice.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index c7924fa9ae..7696fdcd7a 100644 --- a/activesupport/CHANGELOG.md +++ b/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, diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index ed6cd9609a..2bd0a56ea4 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -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. -- cgit v1.2.3