From: shevegen@... Date: 2019-06-22T06:58:29+00:00 Subject: [ruby-core:93313] [Ruby trunk Feature#15950] Allow negative length in `Array#[]`, `Array#[]=`, `String#[]`, `String#[]=` Issue #15950 has been updated by shevegen (Robert A. Heiler). Hmm. I do not necessarily doubt that it may be useful (at the least for some users), but that also actually surprised me. For example: "abcdefgh"[0, -3] # => "fgh" I guess my brain got confused here, since the 0 would put my brain to assume to "start at the most left position". So that code confused me. I am mostly neutral on this though, but have a slight preference to keep the status quo without adding negative length. Either way, it may be a good idea to ask matz because this may have been a old design decision (at the least it seems plausible to me that this may have been in ruby for a long time). ---------------------------------------- Feature #15950: Allow negative length in `Array#[]`, `Array#[]=`, `String#[]`, `String#[]=` https://bugs.ruby-lang.org/issues/15950#change-78786 * Author: sawa (Tsuyoshi Sawada) * Status: Open * 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: