From: Matthew Kerwin Date: 2013-02-15T17:54:08+09:00 Subject: Re: skip iteration in each loop Joel Pearson wrote in post #1097034: > I doubt this is the best method, but here's one way to do it > > irb(main):001:0> persons = [1,2,3,4,5,6,7,8] > => [1, 2, 3, 4, 5, 6, 7, 8] > irb(main):002:0> skip_next = 0 > => 0 > irb(main):003:0> persons.each do |x| > irb(main):004:1* if x == 2 > irb(main):005:2> skip_next = 2 > irb(main):006:2> elsif skip_next > 0 > irb(main):007:2> skip_next -= 1 > irb(main):008:2> next > irb(main):009:2> end > irb(main):010:1> p x > irb(main):011:1> end > 1 > 2 > 5 > 6 > 7 > 8 And, rearranging that slightly: [].tap do |a| skip_next = 0 persons.each do |p| if skip_next > 0 skip_next -= 1 next end a << p skip_next = 2 if p == 2 end end -- Posted via http://www.ruby-forum.com/.