From: eregontp@... Date: 2020-03-07T14:23:43+00:00 Subject: [ruby-core:97402] [Ruby master Misc#16678] Array#values_at has unintuitive behavior when supplied a range starting with negative index Issue #16678 has been updated by Eregon (Benoit Daloze). You can easily achieve wrap-around behavior with: ```ruby > (1..5).to_a.values_at(*(-1..3)) => [5, 1, 2, 3, 4] ``` Using a Range for `values_at` is like taking a slice with `Array#[]/slice`, and Array slices never wrap around (a good thing IMHO, that would be expensive to compute and confusing). ---------------------------------------- Misc #16678: Array#values_at has unintuitive behavior when supplied a range starting with negative index https://bugs.ruby-lang.org/issues/16678#change-84532 * Author: prajjwal (Prajjwal Singh) * Status: Open * Priority: Normal ---------------------------------------- Consider the following: ``` ruby # frozen_string_literal: true a = (1..5).to_a p a.values_at(3..5) # => [4, 5, nil] p a.values_at(-1..3) # => [] ``` When the range begins with a negative `(-1, 0, 1, 2, 3)`, it returns an empty array, which surprised me because I was expecting `[1, 2, 3, 4]`. The argument for this is that it cold be confusing to allow this because the index `-1` could refer to the last argument and it would be unintuitive to return an array `[5, 1, 2, 3, 4]` with jumbled values. The argument against it is that it makes perfect sense to account for this case and return `[nil, 1, 2, 3, 4]`. Opening a dialog to see what others think of this. -- https://bugs.ruby-lang.org/ Unsubscribe: