I want to skip a loop x times according to a condition that is determined at runtime. How can I do this?
for i in (0..5)
if i==0
3.times {next} # i=i+3 also doesnt work
end
puts i
end
Expect to output
3
4
5
EDIT:
To clarify, the question is both the condition (ie i==0) and skipping x times iteration are determined dynamically at runtime, more convoluted example:
condition = Array.new(rand(1..100)).map{|el| rand(1..10000)} #edge cases will bug out
condition.uniq!
for i in (0..10000)
if condition.include? i
rand(1..10).times {next} # will not work
end
puts i
end
(3..5).each {|i| puts i}def skip_first(range, nbr_to_skip).keepersin advance, so you could just writefor i in keepers..... That might be writtencondition = Array.new(rand(1..4)).flat_map do |el| first = rand(1..20); [*first..first+rand(0..4)]; end.uniq # => [7, 8, 9, 18, 6]; keepers = [*1..20]-condition #=> [1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20]. I realize there will be other situations where you will neednext if ....within the block.