Skip to content

Commit

Permalink
Maintain subclasses of Hash when calling Hash#slice.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Apr 12, 2014
1 parent 8fc94e7 commit 08293a4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/i18n/core_ext/hash.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/core_ext/hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down

0 comments on commit 08293a4

Please sign in to comment.