Actions
Feature #13777
closedArray#delete_all
Status:
Rejected
Assignee:
-
Target version:
-
Description
I want Array#delete_if which returns array of deleted values.
For following code,
array = ["a", "aa", "ab", "bb", "c"]
result = {}
until array.empty?
key = array.first
group, array = array.partition { |v| v.start_with?(key) }
result[key] = group
end
result #=> {"a"=>["a", "aa", "ab"], "bb"=>["bb"], "c"=>["c"]}
With Array#delete_all, This would be able to be written in more elegant way like:
array = ["a", "aa", "ab", "bb", "c"]
result = {}
until array.empty?
key = array.first
result[key] = array.delete_all { |v| v.start_with?(key) }
end
result #=> {"a"=>["a", "aa", "ab"], "bb"=>["bb"], "c"=>["c"]}
This is simplified source code of real use case in Haml: https://github.com/haml/haml/blob/923a0d78874fe1d369f8c7a0bf77f67b2c2139bb/lib/haml/attribute_compiler.rb#L75-L76
This grouping task is necessary for Haml optimization.
Do you know simpler way to write this with existing methods?
Actions
Like0
Like0Like0