Project

General

Profile

Actions

Feature #4541

closed

Inconsistent Array.slice()

Added by kbl (Marcin Pietraszek) about 13 years ago. Updated about 6 years ago.

Status:
Rejected
Target version:
-
[ruby-core:<unknown>]

Description

Array slice/[] method is a bit inconsistent. Is it just poorly documented "feature" or a bug? In API doc I can't find this behaviour mentioned as a "special case".

 def test_array_slice
    array = ['a', 'b', 'c']
    assert_equal nil, array[3]
    assert_eaual nil, array[4]
 
    assert_eaual [], array[3, 0] #
    assert_equal nil, array[4, 0] # [] expected (or both nils in array[3, 0] and array[4, 0])
 
    assert_equal ['c'], array[2..2]
    assert_equal [], array[3..3] #
    assert_equal nil, array[4..4] # [] expected (or both nils in array[3..3] and array[4..4])
 end

Same behaviour can be reproduced on ruby 1.8.7 (2010-12-23 patchlevel 330) [x86_64-linux].


Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Bug #4245: Array index with Range inconsistencyRejected01/07/2011Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0