From: "alexeymuranov (Alexey Muranov)" Date: 2012-12-13T06:54:03+09:00 Subject: [ruby-core:50843] [ruby-trunk - Feature #4541] Inconsistent Array.slice() Issue #4541 has been updated by alexeymuranov (Alexey Muranov). stomar (Marcus Stollsteimer) wrote: > Regarding the OP's criticism of poor API documentation, this has in the meantime been improved (with issue #6680). I hope this does not mean that the specification is now based on the implementation :). In my opinion, array = ['a', 'b', 'c'] array[4..4] # => [] makes better sense than array = ['a', 'b', 'c'] array[4..4] # => nil I think that `nil` should only be returned if there is no way to return a meaningful value, but also no particular reason to raise an error. Here a meaningful value to return IMO is `[]`, the same as for `array[3..4]`. Think of a line of small tiles with letters 'a', 'b', 'c' on them which you have enumerated in your head starting with 0 on the left. If you are asked to form a new line with letters 1..5, you will get the line ['b', 'c'] by taking 'b', then 'c', then nothing, and then taking nothing 2 more times. If you are asked to form a new line with letters 3..4, you will get the empty line by taking 2 times nothing. If you are asked to form a new line with letters 4..4, you will get the empty line by taking 1 time nothing. If you are asked to form a new line with letters 4..5, you will get the empty line by taking 2 times nothing. You will always get a line, you will not get "nil". ---------------------------------------- Feature #4541: Inconsistent Array.slice() https://bugs.ruby-lang.org/issues/4541#change-34678 Author: kbl (Marcin Pietraszek) Status: Assigned Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: core Target version: next minor =begin 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]. =end -- http://bugs.ruby-lang.org/