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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top