skip some iteration in ruby for loop -
how can achieve in loop in ruby:
for(int = 1;i<=10;i++){ if (i == 7){ i=9; } #some code here }
i meant it's okay use "next" skip if want jump uncertain iteration via changing variable value itself. far know loop works enumerable type. want loop via index variable, way in c++
there few things can do:
incase want skip few iterations based on defined logic, can use:
next if condition
if intention randomly iterate on whole range, can try this:
(1..10).to_a.shuffle.each { |i| # code }
incase want randomly iterate on chunk of given range, try this:
(1..10).to_a.shuffle.first(n).each { |i| # n number within 1 10 # code }
Comments
Post a Comment