Why Ruby array[array.length, count] returns []? [duplicate]
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.
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".
精彩评论