Вот другой подход.Я все еще не совсем доволен этим - не мог понять, как свести последние два шага в один.Также получилось больше строк, чем в ответе Эндрю Маршалла .Бу.
Спецификация прилагается.
require 'spec_helper'
def report(cycle, order, record)
record.uniq!
order.each_with_index.map do |pattern_list, index|
pattern_list.map do |pattern|
record.each_with_index.inject([]) do |memo, (item, item_index)|
memo.tap do
if pattern =~ item
memo << item
record[item_index] = nil
end
end
end
end.flatten
end.map do |items|
items.each_with_index.group_by do |item, index|
index.div(cycle)
end.map do |ordering, item_with_index|
item_with_index.map(&:first)
end
end
end
describe 'report' do
let(:cycle) { 4 }
let(:order) { [
[/foobar/, /vim/],
[/simple/,/word/, /.*/]
] }
let(:record) {
[ 'vim', 'foobar', 'foo', 'word', 'bar', 'something', 'something1', 'something2', 'something3', 'something4']
}
it "just works" do
report(cycle, order, record.dup).should == [
[["foobar","vim"]],
[["word","foo","bar","something"],["something1","something2","something3","something4"]]
]
end
end