我有一个数组,我只想选择两个指定值之间的元素.
a = ["don't want","don't want","Start","want","Stop","don't want"]
我想调用一个数组上捕获“开始”和“停止”(包括“开始”和“停止”元素)之间的元素的方法.结果数组应如下所示:
[["Start","Stop"],["Start","Stop"]]
class Array def group_by_start_and_stop(start = "Start",stop = "Stop") main_array = [] sub_array = [] group_this_element = false each do |e| group_this_element = true if e == start sub_array << e if group_this_element if group_this_element and e == stop main_array << sub_array sub_array = [] group_this_element = false end end main_array end end
这有效,但对我而言似乎也是不必要的.所以我有两个问题:类似的方法是否已经存在?如果没有,有没有办法使我的group_by_start_and_stop方法更好?