This project is archived and is in readonly mode.

#613 ✓resolved
ronin-17112 (at lighthouseapp)

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

Reported by ronin-17112 (at lighthouseapp) | July 13th, 2008 @ 04:09 PM | in 2.x

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.

Comments and changes to this ticket

  • Repository

    Repository July 17th, 2008 @ 01:28 AM

    • 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...

  • MatthewRudy

    MatthewRudy July 17th, 2008 @ 01:19 PM

    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

  • MatthewRudy

    MatthewRudy July 17th, 2008 @ 01:23 PM

    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
    
  • MatthewRudy

    MatthewRudy July 17th, 2008 @ 01:26 PM

    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
    
  • MatthewRudy

    MatthewRudy July 17th, 2008 @ 02:01 PM

    here's a patch to add the above tests

  • ronin-17112 (at lighthouseapp)

    ronin-17112 (at lighthouseapp) July 17th, 2008 @ 02:50 PM

    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?

  • MatthewRudy

    MatthewRudy July 17th, 2008 @ 03:09 PM

    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))
    
  • Repository

    Repository July 17th, 2008 @ 03:38 PM

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

    This reverts commit 396f9df8916b71f83aad8d56559cf55fc8501679.

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages