Ruby索引赋值

时间:2017-08-29 06:35:18

标签: ruby

我有这段代码:

items = ["place", "key", "holder"]
items[1,0] = ["bottle", "my"]
items # => ["place", "bottle", "my", "key", "holder"]

"my"如何排在第三位?

1 个答案:

答案 0 :(得分:1)

ary[start, length] = obj or other_ary or nil → obj or other_ary or nil

的Ref Array方法
items[1,0] = ["bottle", "my"]
Here, 1 is Index & Length is 0

根据文档'元素在开始时插入到数组中,如果长度为&指数为零。'

对于Ex: -

a = ['A']
a[0, 0] = [ 1, 2 ]          #=> array a will be [1, 2, "A"]

同样,当Index不为零时,&长度为零'元素从给定的索引'

插入到数组中
a = ['A']
a[1, 0] = [ 1, 2 ]          #=> array a will be ["A", 1, 2]