-6

My array is:

array = [:peanut, :butter, :and, :jelly]

array[4,0] gives []

But:

array[5,0] gives nil

I was suspecting array[4,0] should also give nil as the array has only a third index which is :jelly.

Can any Ruby punters out there clarify this behaviour?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
chugh97
  • 9,602
  • 25
  • 89
  • 136

1 Answers1

2

It's all in the documentation:

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.

Community
  • 1
  • 1
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367