Strange behaviour when selecting range of elements from an array?

(1) a = [:x, :y, :z] => [:x, :y, :z]
(2) a[2..-1] => [:z]
(3) a[3..-1] => []
(4) a[4..-1] => nil
(5) a[5..-1] => nil
(6) a[6..-1] => nil

I consider line (3) to be the odd one out here. Given a[3] returns nil,
why shouldn't a[3..-1] also return nil?

Gavin

http://www.ruby-forum.com/topic/1393096#990065

-- Matma Rex

This behavior is (finally) documented in the development version
of Ruby (since 2 days!).

For ary[index], ary[start, length], ary[range], ...:

   "Element Reference --- Returns the element at +index+, or returns a
   subarray starting at the +start+ index and continuing for +length+
   elements, or returns a subarray specified by +range+ of indices."

   "Negative indices count backward from the end of the array (-1 is
   the last element). For +start+ and +range+ cases the starting index
   is just before an element. Additionally, an empty array is returned
   when the starting index for an element range is at the end of
   the array."

   "Returns +nil+ if the index (or starting index) are out of range."

ยทยทยท

Am 07.07.2012 05:32, schrieb Gavin Sinclair:

(1) a = [:x, :y, :z] => [:x, :y, :z]
(2) a[2..-1] => [:z]
(3) a[3..-1] =>
(4) a[4..-1] => nil
(5) a[5..-1] => nil
(6) a[6..-1] => nil

I consider line (3) to be the odd one out here. Given a[3] returns nil,
why shouldn't a[3..-1] also return nil?

Gavin

--
<https://github.com/stomar/&gt;