Question

I'm trying to understand the difference between using Process.exit(0) versus break to exit a loop in Ruby. Can someone please explain?

Était-ce utile?

La solution

A break will just escape out of it's own scope. Imagine this:

loop do
  #something goes here
  break
end

In the above example the break scope is it's direct loop so the program will stop when that break is executed.

Now imagine this example:

loop do  #loop 1
  loop do   #loop 2
    #something goes here
    break
  end
end

In the above code break will only exit loop #2 and the rest of the code will continue to run, whereas Process.exit will terminate the entire script.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top