Forum: Ruby-core Inconsistent Array.slice()

Posted by Matthew Kerwin (mattyk)
on 2012-12-13 02:11
(Received via mailing list)
Issue #4541 has been updated by phluid61 (Matthew Kerwin).


=begin
 alexeymuranov (Alexey Muranov) wrote:
 > 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 :).

Actually it kind of is.  Hence the term "reference implementation."

One other thing to consider is that (({nil})) is falseish, but (({[]})) 
is trueish.

  !!nil #=> false
  !![]  #=> true

Your change could break existing logic.  I know it's relatively trivial 
to use (({array[range].empty?})) but that's still a change that every 
developer potentially needs to make to the existing codebase.
=end

----------------------------------------
Feature #4541: Inconsistent Array.slice()
https://bugs.ruby-lang.org/issues/4541#change-34681

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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.