From: eregontp@... Date: 2019-06-22T07:04:46+00:00 Subject: [ruby-core:93314] [Ruby trunk Feature#15950] Allow negative length in `Array#[]`, `Array#[]=`, `String#[]`, `String#[]=` Issue #15950 has been updated by Eregon (Benoit Daloze). Status changed from Open to Rejected sawa (Tsuyoshi Sawada) wrote: > But to take the last n characters, we need to use n in two arguments: in the index (in negative form) in addition to the length: > > ```ruby > "abcdefgh"[-3, 3] # => "fgh" > ``` Why not: ```ruby "abcdefgh"[-3..-1] # => "fgh" ``` I would think the Range form is more idiomatic for this usage. Also, with endless ranges, we can just do: ```ruby "abcdefgh"[-3..] # => "fgh" ``` I find the negative length very confusing (I can't even understand the example above, rotation should not be part of indexing IMHO), and I'm negative on making indexing semantics a lot more complicated. So I think are already good ways to achieve this, and I don't see a rationale to include this complicated feature, so I would like to reject this ticket. Feel free to motivate why it's better than the existing 2 alternate syntax, though. ---------------------------------------- Feature #15950: Allow negative length in `Array#[]`, `Array#[]=`, `String#[]`, `String#[]=` https://bugs.ruby-lang.org/issues/15950#change-78787 * Author: sawa (Tsuyoshi Sawada) * Status: Rejected * Priority: Normal * Assignee: * Target version: ---------------------------------------- To take the first n characters of a string, using `[]` is straightforward: ```ruby "abcdefgh"[0, 3] # => "abc" ``` But to take the last n characters, we need to use n in two arguments: in the index (in negative form) in addition to the length: ```ruby "abcdefgh"[-3, 3] # => "fgh" ``` This is cumbersome. I wish negative length to be allowed, and be interpreted as measuring leftward (while cycling the receiver if necessary). ```ruby "abcdefgh"[0, -3] # => "fgh" "abcdefgh"[5, -3] # => "cde" ``` If there is not enough characters or elements, it should stop at the boundary. ```ruby "abcdefgh"[1, -3] # => "a" ``` -- https://bugs.ruby-lang.org/ Unsubscribe: