From fefe3770509b4468bdd0ee67d2905df1bba60e1a Mon Sep 17 00:00:00 2001 From: adam Date: Tue, 23 Sep 2008 11:57:57 +0200 Subject: [PATCH] Adds failed test case for slicing hash with indifferent access with symbol keys --- activesupport/test/core_ext/hash_ext_test.rb | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-) diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 44d48e7..9537f48 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -329,6 +329,16 @@ class HashExtTest < Test::Unit::TestCase end end + def test_indifferent_slice_access_with_symbols + original = {'login' => 'bender', 'password' => 'shiny', 'stuff' => 'foo'} + original = original.with_indifferent_access + + slice = original.slice(:login, :password) + + assert_equal 'bender', slice[:login] + assert_equal 'bender', slice['login'] + end + def test_except original = { :a => 'x', :b => 'y', :c => 10 } expected = { :a => 'x', :b => 'y' } -- 1.5.4.3 From 6b0c5a68d0200e390a399998a7930d161fa2fbe7 Mon Sep 17 00:00:00 2001 From: adam Date: Tue, 23 Sep 2008 12:08:24 +0200 Subject: [PATCH] slice now returns indifferent hash if called on one --- .../lib/active_support/core_ext/hash/slice.rb | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 3f14470..88df49a 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -18,7 +18,7 @@ module ActiveSupport #:nodoc: # Returns a new hash with only the given keys. def slice(*keys) keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) - hash = {} + hash = self.class.new keys.each { |k| hash[k] = self[k] if has_key?(k) } hash end -- 1.5.4.3