[ruby-core:93308] [Ruby trunk Feature#15950] Allow negative length in `Array#[]`, `Array#[]=`, `String#[]`, `String[]=`

From: sawadatsuyoshi@...
Date: 2019-06-22 03:09:44 UTC
List: ruby-core #93308
Issue #15950 has been reported by sawa (Tsuyoshi Sawada).

----------------------------------------
Feature #15950: Allow negative length in `Array#[]`, `Array#[]=`, `String#[]`, `String[]=`
https://bugs.ruby-lang.org/issues/15950

* 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"
```




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next