This project is archived and is in readonly mode.
Slice and Slice! don't accept an array of keys
-
Repository
- State changed from new to resolved
(from [396f9df8916b71f83aad8d56559cf55fc8501679]) Hash#slice supports an array of keys [#613 state:resolved]
Signed-off-by: Joshua Peek
-
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
-
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 -
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 -
MatthewRudy
here's a patch to add the above tests
-
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?
-
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)) -
Repository
(from [b3a2ee7b87a6b2a4c6ff086644f40a472a676b65]) Revert "Hash#slice supports an array of keys [#613 state:resolved]"
This reverts commit 396f9df8916b71f83aad8d56559cf55fc8501679.
