Why does the Ruby array [array.length, count] return []?

Possible duplicate:
Is there any invisible Array interrupt in Ruby?
Array lattice in Ruby: looking for explanations for illogical behavior (taken from Rubykoans.com)

a = %w[a b c]
a[3, 1]    # => []
a[4, 1]    # => nil

Can someone explain why a [3, 1] returns [] ? Why instead of nil ?

Thank.

+5
source share
1 answer

Well, it looks like Ruby core documentation , mark this as a "special case." According to the Ruby programming language (O'Reilly, 2008), a comment on this case:

a[arr_len, len] #=> [], empty array right at the end
a[arr_len + 1, len] #=> nil, nonthing beyond that

. , " ".

+3

All Articles