嗨,我有一个小的 ruby 函数,它可以拆分一个 Ruby 数组,如下所示:-
def rearrange arr,from,to
sidx = arr.index from
eidx = arr.index to
arr[sidx] = arr[sidx+1..eidx]
end
arr= ["Red", "Green", "Blue", "Yellow", "Cyan", "Magenta", "Orange", "Purple", "Pink", "White", "Black"]
start = "Yellow"
stop = "Orange"
rearrange arr,start,stop
puts arr.inspect
#=> ["Red", "Green", "Blue", ["Cyan", "Magenta", "Orange"], "Cyan", "Magenta", "Orange", "Purple", "Pink", "White", "Black"]
我需要在开始和停止搜索中使用正则表达式,例如
开始 = "/Yell/"
停止 = "/Ora/"
有没有一种简单的方法可以在 Ruby 中做到这一点?