3

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.

Why does

a = [0,1,2,3,4]
a[4] #=> 4
a[4,0] #=> [] - length is 0, so empty array is returned

a[5] #=> nil - makes sense since it is out of range
a[5,0] #=> [] - why is this empty?
a[6,0] #=> nil - but this is nil?
Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244

1 Answers1

2

I put in {ix} for the positions instead of commas.

a = [{ix0}0{ix1}1{ix2}2{ix3}3{ix4}4{ix5}]

a[5,0] means go to 5 {ix5} and take 0 elements. That is just an empty array.

{ix6} is not a valid starting positions, thus nil.

hirolau
  • 13,451
  • 8
  • 35
  • 47