Skip to content
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

Fix Hash#slice behavior not to return a subclass instance #390

Merged
merged 1 commit into from Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/i18n/core_ext/hash.rb
@@ -1,6 +1,6 @@
class Hash
def slice(*keep_keys)
h = self.class.new
h = {}
keep_keys.each { |key| h[key] = fetch(key) if has_key?(key) }
h
end unless Hash.method_defined?(:slice)
Expand All @@ -21,7 +21,7 @@ def deep_symbolize_keys
MERGER = proc do |key, v1, v2|
Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : v2
end

def deep_merge!(data)
merge!(data, &MERGER)
end unless Hash.method_defined?(:deep_merge!)
Expand Down
6 changes: 0 additions & 6 deletions test/core_ext/hash_test.rb
Expand Up @@ -20,12 +20,6 @@ class I18nCoreExtHashInterpolationTest < I18n::TestCase
assert_equal expected, hash.slice(:foo, :not_here)
end

test "#slice maintains subclasses of Hash" do
klass = Class.new(Hash)
hash = klass[:foo, 'bar', :baz, 'bar']
assert_instance_of klass, hash.slice(:foo)
end

test "#except" do
hash = { :foo => 'bar', :baz => 'bar' }
expected = { :foo => 'bar' }
Expand Down