ブロックn
の実行中に反復をスキップすることはできますか?each
persons.each_cons(2) do |person|
if person[0] == person[1]
#SKIP 2 iterations
end
puts "Howdy? #{person[0]}"
end
ブロックn
の実行中に反復をスキップすることはできますか?each
persons.each_cons(2) do |person|
if person[0] == person[1]
#SKIP 2 iterations
end
puts "Howdy? #{person[0]}"
end
これを行う別の方法は、実際には ruby とは異なりますが、each
反復子を使用することです。
persons = [1,1,2,2,2,3,4,4,5,5,6,7,8,9]
persons_iterator = persons.each
begin
while first_person = persons_iterator.next do
second_person = persons_iterator.next
if first_person == second_person
persons_iterator.next.next # Skip 2 iterations
end
puts "Howdy? #{first_person}"
end
rescue StopIteration
# Iterator end
end