This is an example from the Ruby documentation for the Array class
这是Array类的Ruby文档中的一个示例
arr = [1, 2, 3, 4, 5, 6]
arr[2, 3]
=> [3, 4, 5]
arr[1, 2]
=> [2, 3]
Umm, what? Help, anyone? Why this discrepancy between the [2,3] and [1,2] behavior?
嗯,什么?帮忙,有人吗?为什么[2,3]和[1,2]行为之间存在这种差异?
3 个解决方案
#1
2
Read the bold line taken from the documentation of Accessing Elements
:
阅读访问元素文档中的粗线:
Elements in an array can be retrieved using the #[] method. It can take a single integer argument (a numeric index), a pair of arguments (start and length) or a range.
可以使用#[]方法检索数组中的元素。它可以采用单个整数参数(数字索引),一对参数(开始和长度)或范围。
When you are using a pair of arguments (start and length),it will return a subarray starting at the start index and continuing for length elements.
当您使用一对参数(开始和长度)时,它将返回从起始索引开始并继续长度元素的子数组。
In the line arr[1, 2]
- 1 is a starting index and 2 is the length. Means you are telling starting from index 1
of the arr
,you want to fetch 2 elements.
在行arr [1,2]中 - 1是起始索引,2是长度。意味着你要从arr的索引1开始讲述,你想要获取2个元素。
In the line arr[2, 3]
- 2 is a starting index and 3 is the length. Means you are telling starting from index 2
of the arr
,you want to fetch 3 elements.
在行arr [2,3]中,2是起始索引,3是长度。意味着你要从arr的索引2开始讲述,你想要获取3个元素。
Read Array#[]
also.
同时读取数组#[]。
#2
1
There is no discrepancy here (Ruby array index starts with zero)
这里没有差异(Ruby数组索引从零开始)
arr = [1, 2, 3, 4, 5, 6]
(0, 1, 2, 3, 4, 5) #=> their index/positions
arr[2,3] #=> arr[start,length]- start at 2nd index; it has value 3 and get 3 elements [3,4,5]
arr[1,2] #=> arr[start,length]- start at 1st index; it has value 2 and get 2 elements [2,3]
#3
0
It might be easier to see if you are using an array of letters.
您可能更容易看到是否使用了一组字母。
>> arr = %w[a b c d e f g]
=> ["a", "b", "c", "d", "e", "f", "g"]
>> arr[2, 3]
=> ["c", "d", "e"]
>> arr[1, 2]
=> ["b", "c"]
The two prior descriptions apply here, but you have less of a chance for not translating what you are asking for and what you are getting.
这两个先前的描述适用于此,但您没有机会不翻译您要求的内容以及您获得的内容。
Index of an array starts at 0, so when asking for the 3 elements starting from index 2, you are getting c, d and e.
数组的索引从0开始,所以当从索引2开始询问3个元素时,你得到的是c,d和e。
When you are asking for 2 elements starting from index 1, you get b and c.
当你要求从索引1开始的2个元素时,你得到b和c。
#1
2
Read the bold line taken from the documentation of Accessing Elements
:
阅读访问元素文档中的粗线:
Elements in an array can be retrieved using the #[] method. It can take a single integer argument (a numeric index), a pair of arguments (start and length) or a range.
可以使用#[]方法检索数组中的元素。它可以采用单个整数参数(数字索引),一对参数(开始和长度)或范围。
When you are using a pair of arguments (start and length),it will return a subarray starting at the start index and continuing for length elements.
当您使用一对参数(开始和长度)时,它将返回从起始索引开始并继续长度元素的子数组。
In the line arr[1, 2]
- 1 is a starting index and 2 is the length. Means you are telling starting from index 1
of the arr
,you want to fetch 2 elements.
在行arr [1,2]中 - 1是起始索引,2是长度。意味着你要从arr的索引1开始讲述,你想要获取2个元素。
In the line arr[2, 3]
- 2 is a starting index and 3 is the length. Means you are telling starting from index 2
of the arr
,you want to fetch 3 elements.
在行arr [2,3]中,2是起始索引,3是长度。意味着你要从arr的索引2开始讲述,你想要获取3个元素。
Read Array#[]
also.
同时读取数组#[]。
#2
1
There is no discrepancy here (Ruby array index starts with zero)
这里没有差异(Ruby数组索引从零开始)
arr = [1, 2, 3, 4, 5, 6]
(0, 1, 2, 3, 4, 5) #=> their index/positions
arr[2,3] #=> arr[start,length]- start at 2nd index; it has value 3 and get 3 elements [3,4,5]
arr[1,2] #=> arr[start,length]- start at 1st index; it has value 2 and get 2 elements [2,3]
#3
0
It might be easier to see if you are using an array of letters.
您可能更容易看到是否使用了一组字母。
>> arr = %w[a b c d e f g]
=> ["a", "b", "c", "d", "e", "f", "g"]
>> arr[2, 3]
=> ["c", "d", "e"]
>> arr[1, 2]
=> ["b", "c"]
The two prior descriptions apply here, but you have less of a chance for not translating what you are asking for and what you are getting.
这两个先前的描述适用于此,但您没有机会不翻译您要求的内容以及您获得的内容。
Index of an array starts at 0, so when asking for the 3 elements starting from index 2, you are getting c, d and e.
数组的索引从0开始,所以当从索引2开始询问3个元素时,你得到的是c,d和e。
When you are asking for 2 elements starting from index 1, you get b and c.
当你要求从索引1开始的2个元素时,你得到b和c。