开发者

Why Ruby array[array.length, count] returns []? [duplicate]

This question already has answers here: Closed 11 years ago.

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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜