5

Possible Duplicate:
Is there some kind of unseen Array termination in Ruby?
Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)

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

Could anyone explain why a[3, 1] returns []? Why not nil instead?

Thank you.

Community
  • 1
  • 1
Sambath Prum
  • 1,858
  • 9
  • 25
  • 33

1 Answers1

3

Well, looks like Ruby core documentation only mark this as "special case". According to The Ruby Programming Language(O'Reilly,2008), the comment on this case is:

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

No further explanation is given. So I think you should just remember the "special case".

venj
  • 563
  • 1
  • 3
  • 14