Skip to content

Commit

Permalink
Maintain subclasses of Hash when calling Hash#slice.
Browse files Browse the repository at this point in the history
Fixes #250
  • Loading branch information
jimmycuadra authored and radar committed Nov 17, 2016
1 parent 14b6a5c commit 6f291c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/i18n/core_ext/hash.rb
@@ -1,6 +1,6 @@
class Hash
def slice(*keep_keys)
h = {}
h = self.class.new
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: 6 additions & 0 deletions test/core_ext/hash_test.rb
Expand Up @@ -20,6 +20,12 @@ 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

0 comments on commit 6f291c4

Please sign in to comment.