Project

General

Profile

Misc #19131

Updated by andrykonchin (Andrew Konchin) almost 2 years ago

I've noticed a strange nuance and not sure whether it's intentional or not. In case it's intentional - it seems to me inconsistent. 

 I will illustrate it with the following example: 

 ```ruby 
 /(.)(.)(.)(.)/.match("abcde").values_at(-5..-1) # => ["abcd", "a", "b", "c", "d"] 
 /(.)(.)(.)(.)/.match("abcde").values_at(-5) # => [nil] 
 ``` 

 So with a negative index we can address the whole matched string (`0` element) with a Range argument but cannot with an Integer index. 

 We can index with negative values only captured values: 

 ```ruby 
 /(.)(.)(.)(.)/.match("abcde").values_at(-4) # => ["a"] 
 /(.)(.)(.)(.)/.match("abcde").values_at(-3) # => ["b"] 
 /(.)(.)(.)(.)/.match("abcde").values_at(-2) # => ["c"] 
 # ... 
 ``` 

 I would expect `Range` Range and `Integer` arguments are handled Integer should work consistently and either both allow to address the first element with negative index or both disallow it. 

 --- 

 ``` 
 ruby -v 
 ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin21] 
 ```

Back