0

Is there a way to make a Ruby program keep executing until the program has an error? I want my loop to stop when the program returns an error. Thanks

1
  • You should state the error's form, for example, an exception or a 'false' returning. As they'll have different handling methods. For a infinite loop, i suppose the Kernel#loop would be better than 'while true'.
    – shouya
    May 16, 2012 at 13:50

2 Answers 2

2

A infinite loop can help?

while true do
    your code
end

If your code throw an error the loop stops.

1
  • Interesting... I just looked it up and the do is optional. I don't think I've ever seen it there before.
    – dontangg
    May 15, 2012 at 5:26
2

This is another example. Will run infinite times till exception comes and also handles your exception and then exit form code.

inc = 5
while true do
  begin
puts 4/inc
inc-=1
  rescue Exception=> e
    puts e
    exit
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.