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

Maintain subclasses of Hash when calling Hash#slice #250

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion 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)
Expand Down
6 changes: 6 additions & 0 deletions test/core_ext/hash_test.rb
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