Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Slice and Slice! don't accept an array of keys

#613

Currently slice and slice! don't worry if you pass yours keys as an array ([:a, :b]) since the slice method doesn't flatten the array down and the args use a splat (*).

This patch includes the test and I also have this applied to one of my production apps.

Reported by ronin-17112 (at lighthouseapp) · July 13th, 2008 @ 04:09 PM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Repository
    Repository
    • State changed from new to resolved

    (from [396f9df8916b71f83aad8d56559cf55fc8501679]) Hash#slice supports an array of keys [#613 state:resolved]

    Signed-off-by: Joshua Peek

    http://github.com/rails/rails/co...

    July 17th, 2008 @ 01:28 AM

  2. MatthewRudy
    MatthewRudy

    reiterating,

    this is bad.

    if you want to pass it an array,

    you should be able to pass it an array,

    if you want to splat it,

    splat it

    hash.slice(*[:a,:b]) if you mean slice(:a, :b)

    or hash.slice([:a,:b]) if that's a key

    July 17th, 2008 @ 01:19 PM

  3. MatthewRudy
    MatthewRudy

    and here's a perversion of the attached test.

    def test_slice_with_array_keys
      original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
      expected = { [:a, :b] => "an array key", :c => 10 }
    
      # Should return a new hash with only the given keys, when given an array as a key.
      assert_equal expected, original.slice([:a, :b], :c)
      assert_not_equal expected, original
    
      # Should replace the hash with only the given keys, when given an array of keys.
      assert_equal expected, original.slice!([:a, :b], :c)
      assert_equal expected, original
    end
    

    July 17th, 2008 @ 01:23 PM

  4. MatthewRudy
    MatthewRudy
    def test_slice_with_splatted_keys
      original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
      expected = { :a => 'x', :b => "y" }
    
      # Should grab each of the splatted keys.
      assert_equal expected, original.slice(*[:a, :b])
    end
    

    July 17th, 2008 @ 01:26 PM

  5. MatthewRudy
    MatthewRudy

    here's a patch to add the above tests

    July 17th, 2008 @ 02:01 PM

  6. ronin-17112 (at lighthouseapp)
    ronin-17112 (at lighthouseapp)

    MatthewRudy,

    You are correct sir. I didn't realize a hash could have a key that was an array. I don't see a way to clarify the intentions of the method's user based on the current footprint of the method. I suppose we could just clean up the docs a bit to point out that you can use the *splat to pass an array that you don't want used for a single hash key?

    July 17th, 2008 @ 02:50 PM

  7. MatthewRudy
    MatthewRudy

    yeah.

    here's a patch with the tests I added earlier

    and documentation on that specific use.

          # If you have an array of keys you want to limit to, you should splat them:
          #
          #   valid_keys = [:mass, :velocity, :time]
          #   search(options.slice(*valid_keys))
    

    July 17th, 2008 @ 03:09 PM

  8. Repository
    Repository

    (from [b3a2ee7b87a6b2a4c6ff086644f40a472a676b65]) Revert "Hash#slice supports an array of keys [#613 state:resolved]"

    This reverts commit 396f9df8916b71f83aad8d56559cf55fc8501679.

    http://github.com/rails/rails/co...

    July 17th, 2008 @ 03:38 PM