[#66126] Creation/Conversion methods/functions table for Ruby types — SASADA Koichi <ko1@...>
Hi,
5 messages
2014/11/07
[#66248] [ruby-trunk - Feature #10423] [PATCH] opt_str_lit*: avoid literal string allocations — normalperson@...
Issue #10423 has been updated by Eric Wong.
3 messages
2014/11/13
[#66595] [ruby-trunk - Bug #10557] [Open] Block not given when the argument is a string — bartosz@...
Issue #10557 has been reported by Bartosz Kopinski.
3 messages
2014/11/30
[ruby-core:66395] [ruby-trunk - Feature #10523] [Rejected] Suggestion for new Array.delete_to method
From:
matz@...
Date:
2014-11-21 15:40:48 UTC
List:
ruby-core #66395
Issue #10523 has been updated by Yukihiro Matsumoto.
Status changed from Open to Rejected
I prefer Array#partition for its immutability.
Matz.
----------------------------------------
Feature #10523: Suggestion for new Array.delete_to method
https://bugs.ruby-lang.org/issues/10523#change-50035
* Author: Gary Weaver
* Status: Rejected
* Priority: Normal
* Assignee:
* Category:
* Target version:
----------------------------------------
Array's delete and delete_at return the deleted item from an array, so it would seem as though Array should allow some sort of delete method that could take a block and would return the deleted items that matched as an array.
Currently the hack for this posted at http://stackoverflow.com/a/5480449/178651 is to use delete_if, but have to store the matched value to delete in a separate array, e.g.:
reject = []
=> []
content = [1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
content.delete_if {|v| reject << v if v > 5}
=> [1, 2, 3, 4, 5]
reject
=> [6, 7, 8, 9]
However, what if there were a more elegant way of doing this, like:
content = [1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
content.delete_to {|v| if v > 5}
=> [6, 7, 8, 9]
And you could also store the delete values in an existing array like:
content = [1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
content.delete_to([:a, :b]) {|v| if v > 5}
=> [:a, :b, 6, 7, 8, 9]
Or to remove and transfer the entire contents of one array to another array:
a = [1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = []
=> []
content.delete_to(b)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
a
=> []
b
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
In which case, delete_to could be aliased as move_to.
The primary use case though is the first-it would be helpful to matching items from an array, remove them from that array, and return the removed items.
--
https://bugs.ruby-lang.org/