Ruby/Statement/retry

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

retry restarts the iteration

n = 10
n.times do |x|   
  print x        
  if x == 9      
    n -= 1       # Decrement n (we won"t reach 9 the next time!)
    retry        # Restart the iteration
  end
end



The retry statement will start a loop or iterator over entirely.

# That can be useful if you need to re-run a particular loop iteration.
 
1.upto(10) do |number|
  puts "1/" + number.to_s + " = " + (1 / Float(number)).to_s
  print "Enter r to retry: "
  gets
  chomp
  retry if $_ == "r"
end