0
做一個紅寶石問題和思考自己,必須有一個更簡單的方法。我想將一個指定索引的數組拆分爲兩個子數組。第一個子數組包含指定索引號的數字,第二個子數組包含指定索引之後的數字。Ruby,是否有內置方法將數組拆分爲指定索引處的兩個子數組?
例
([2, 1, 2, 2, 2],2) => [2,1] [2,2,2]
#here, the specified index is two, so the first array is index 0 and 1,
the second is index 2, 3, and 4.
def array_split(arr,i)
arr1 = []
arr2 = []
x = 0
while x < arr.count
x < i ? arr1 << arr[x] : arr2 << arr[x]
x += 1
end
return arr1, arr2
end
這是while循環沒有問題。我想知道是否有更簡單的解決方案。