Sna*_*arf 3 ruby arrays slice multidimensional-array
可以说我有一个像这样的数组: ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
我不知道数组的长度或何时会出现'x'.当我到达'x'时,我想将以下元素推送到一个新数组中,直到我到达下一个元素includes?('x').
includes?('x')
期望的输出是: [['cat', 'dog']['dolphin','cougar', 'whale']]
[['cat', 'dog']['dolphin','cougar', 'whale']]
我怎样才能做到这一点?
dbe*_*hur 7
Enumerable#slice_before 这很简单:
Enumerable#slice_before
a = ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale'] a.slice_before(/\Ax\z/).map { |chunk| chunk.drop(1) } => [["cat", "dog"], ["dolphin", "cougar", "whale"]]
归档时间:
13 年,4 月 前
查看次数:
152 次
最近记录:
10 年,6 月 前