Project

General

Profile

Actions

Bug #18741

closed

Slicing an Array using index out of range inconsistent return type

Added by leandrodesouzadev (Leandro de Souza) about 2 years ago. Updated about 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]
[ruby-core:108282]

Description

Hi there!
I've been learning ruby and following the Koans path. On the arrays part i get the following test case to handle:

  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


Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Feature #16822: Array slicing: nils and edge casesRejectedActions

Updated by zverok (Victor Shepelev) about 2 years ago

I already raised the problem in #16822, it was closed by @matz (Yukihiro Matsumoto) with

I don't think the benefit of changing outweighs the pain of incompatibility. Rejected.

Updated by ccmywish (Aoran Zeng) about 2 years ago

HI @zverok (Victor Shepelev), I think your proposition is a little different from what's reported here.

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 ?

Actions #3

Updated by jeremyevans0 (Jeremy Evans) about 2 years ago

  • Is duplicate of Feature #16822: Array slicing: nils and edge cases added
Actions #4

Updated by jeremyevans0 (Jeremy Evans) about 2 years ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0