public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Add extra tests to ensure Hash#slice works with an array as a key. #613

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
matthewrudy (author)
Thu Jul 17 05:58:42 -0700 2008
lifo (committer)
Thu Jul 17 07:34:51 -0700 2008
commit  7e8aee7e6cbd23c1eb18bec1869465e923915e7a
tree    03a8ae8f0e11c58683ee12491935986d55302674
parent  b3a2ee7b87a6b2a4c6ff086644f40a472a676b65
...
9
10
11
 
 
 
 
 
12
13
14
...
9
10
11
12
13
14
15
16
17
18
19
0
@@ -9,6 +9,11 @@ module ActiveSupport #:nodoc:
0
       #   end
0
       #
0
       #   search(options.slice(:mass, :velocity, :time))
0
+      #
0
+      # If you have an array of keys you want to limit to, you should splat them:
0
+      #
0
+      #   valid_keys = [:mass, :velocity, :time]
0
+      #   search(options.slice(*valid_keys))
0
       module Slice
0
         # Returns a new hash with only the given keys.
0
         def slice(*keys)
...
292
293
294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
296
297
...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
0
@@ -292,6 +292,27 @@ class HashExtTest < Test::Unit::TestCase
0
     assert_equal expected, original
0
   end
0
 
0
+  def test_slice_with_an_array_key
0
+    original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
0
+    expected = { [:a, :b] => "an array key", :c => 10 }
0
+
0
+    # Should return a new hash with only the given keys when given an array key.
0
+    assert_equal expected, original.slice([:a, :b], :c)
0
+    assert_not_equal expected, original
0
+
0
+    # Should replace the hash with only the given keys when given an array key.
0
+    assert_equal expected, original.slice!([:a, :b], :c)
0
+    assert_equal expected, original
0
+  end
0
+
0
+  def test_slice_with_splatted_keys
0
+    original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
0
+    expected = { :a => 'x', :b => "y" }
0
+
0
+    # Should grab each of the splatted keys.
0
+    assert_equal expected, original.slice(*[:a, :b])
0
+  end
0
+
0
   def test_indifferent_slice
0
     original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access
0
     expected = { :a => 'x', :b => 'y' }.with_indifferent_access

Comments