From: Rodrigo Rosenfeld Rosas Date: 2011-06-30T11:09:53+09:00 Subject: [ruby-core:37679] [Ruby 1.9 - Feature #4541] Inconsistent Array.slice() Issue #4541 has been updated by Rodrigo Rosenfeld Rosas. What I think is less confusing is to always return an array when passing ranges, like the following example:
[][1..2] #=> [] instead of nil
Think in some practical method like this one:
def some_method(*elements) # suppose elements can be [], maybe elements are read from some file...
  special_element = args[0] # suppose we don't want elements.shift because we will send elements to another method...
  do_something_special if special_element
  elements[1..-1].each{|el| ...} # this will raise an exception if elements == []
  call_another_method(elements)
end
Yes, I know this is not a great example, but I'm too tired right now to think in a better one... ---------------------------------------- Feature #4541: Inconsistent Array.slice() http://redmine.ruby-lang.org/issues/4541 Author: Marcin Pietraszek Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: core Target version: 1.9.x =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://redmine.ruby-lang.org