有没有办法获得迭代器的位置(它迭代了多少次)?
values = (1..100)
values.each do |value|
if ... % 10 == 1 then puts iterator.count end
end
还是您必须明确计算:
values = (1..100)
counter = 0
values.each do |value|
if counter % 10 == 1 then puts counter end
counter += 1
end
有更清洁的方法吗?
红宝石 1.9.2