From: "ccmywish (Aoran Zeng)" Date: 2022-04-19T02:51:51+00:00 Subject: [ruby-core:108285] [Ruby master Bug#18741] Slicing an Array using index out of range inconsistent return type Issue #18741 has been updated by ccmywish (Aoran Zeng). HI @zverok, I think your proposition is a little different from what's reported here. ```ruby array = [:peanut, :butter, :and, :jelly] # 4 is out of range, but -> [] array[4,0] # 5 is out of range, but -> nil array[5,0] ``` Both out or range, why one `[]`, one `nil` ? ---------------------------------------- Bug #18741: Slicing an Array using index out of range inconsistent return type https://bugs.ruby-lang.org/issues/18741#change-97304 * Author: leandrodesouzadev (Leandro de Souza) * Status: Open * Priority: Normal * ruby -v: ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- Hi there! I've been learning ruby and following the Koans path. On the arrays part i get the following test case to handle: ``` ruby def test_slicing_arrays array = [:peanut, :butter, :and, :jelly] assert_equal [:peanut], array[0,1] assert_equal [:peanut, :butter], array[0,2] assert_equal [:and, :jelly], array[2,2] assert_equal [:and, :jelly], array[2,20] assert_equal [], array[4,0] # Out of range, returns empty array assert_equal [], array[4,100] assert_equal nil, array[5,0] # Also out of range, but returns nil end ``` It feels strange when an slice out of the range by one (1) returns an empty array, but when out of the range by two (2) returns nil. I expected that the slice should either return nil/empty on both cases -- https://bugs.ruby-lang.org/ Unsubscribe: