我有一些看起来像这样的嵌套迭代器:
line_count = 1
if commit.body.lines.each do |line|
unless @diff_line_nums.include? line_count
puts line
else
filtered_diffs.each do |diff|
diff.body.lines.each do |line|
puts line
end
end
end
line_count += 1
end
简单来说,这就是我当前版本中发生的情况:
a line_count = 1
b line_count = 2
c - line_count = 3
1
2
3
d line_count = 4
e - line_count = 5
1
2
3
4
5
f line_count = 6
g line_count = 7
h line_count = 8
i line_count = 9
j line_count = 10
然而,我想要发生的是:
a line_count = 1
b line_count = 2
c - line_count = 3
1
2
3
4
h line_count = 8
i line_count = 9
j - line_count = 10
1
2
3
n line_count = 14
o line_count = 15
p line_count = 16
Aka ....当它在外部块中迭代时,一旦它进入内部迭代并成功处理每个项目,在内部的每次成功迭代时它也应该自动增加外部。
答案 0 :(得分:0)
您可以使用第二个变量来保存其他计数器。像这样:
line_count = 1
shifted_line_count = 0
if commit.body.lines.each do |line|
unless @diff_line_nums.include? line_count
puts line + shifted_line_count
else
filtered_diffs.each do |diff|
diff.body.lines.each do |line|
shifted_line_count += 1
puts line
end
end
end
line_count += 1
end