在红宝石阵列切片返回不同的结果 - 零对空数组 [英] Slicing of arrays in ruby returns different result - nil vs. empty array

查看:127
本文介绍了在红宝石阵列切片返回不同的结果 - 零对空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过,如果一个数组是在它长度切片,它返回一个空数组的事实疑惑,但在长度大于,它返回。究竟是什么原因呢?例如:

I'm puzzled by the fact that, if an array is sliced at it length, it returns an empty array, but at a length greater than that, it returns nil. What is the reason for this? For example:

arr = [1,2,3,4,5]

这样做,其中 Y'GT; arr.length X 是任意正整数,返回

Doing this, where y > arr.length and x is any positive integer, returns nil:

arr[y, x] # => nil

但这样做下面的返回 []

arr[5, x] # => []

由于改编[5] 不存在,不应该将其返回以及

Since arr[5] doesn't exist, shouldn't it return nil as well?

推荐答案

这一切都在文档中:的 http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-5B-5D

It's all in the documentation: http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-5B-5D

此外,则返回一个空数组时为元素范围的起始索引是在数组的结尾。

Additionally, an empty array is returned when the starting index for an element range is at the end of the array.

返回nil如果指数(或起始索引)超出范围。

Returns nil if the index (or starting index) are out of range.

这篇关于在红宝石阵列切片返回不同的结果 - 零对空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆