0

While going over the Array class from the Ruby 2.0.0 docs, I noticed something I can't explain. The following is a direct example from the Ruby Docs:

a = [ "a", "b", "c", "d", "e" ]
a[6, 1]                #=> nil
a[5]                   #=> nil
a[5, 1]                #=> []

Could some one explain to me why a[5, 1] has the output [ ]?

the Tin Man
  • 155,156
  • 41
  • 207
  • 295
jmoon90
  • 329
  • 4
  • 17

1 Answers1

2

The semantics of [] are the same as for slice when two integers are provided. As explained in http://ruby-doc.org/core-2.0/Array.html#method-i-slice, when the first integer points to the end of the array, the empty array will be returned.

Peter Alfvin
  • 27,785
  • 8
  • 64
  • 103