diff --git a/lib/i18n/core_ext/hash.rb b/lib/i18n/core_ext/hash.rb index f2a2422b..955e0143 100644 --- a/lib/i18n/core_ext/hash.rb +++ b/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) } h end unless Hash.method_defined?(:slice) diff --git a/test/core_ext/hash_test.rb b/test/core_ext/hash_test.rb index 25c57488..46884473 100644 --- a/test/core_ext/hash_test.rb +++ b/test/core_ext/hash_test.rb @@ -14,6 +14,12 @@ class I18nCoreExtHashInterpolationTest < Test::Unit::TestCase assert_equal expected, hash.slice(:foo) 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' }