break

You can use break to break out of a while loop:

  1. a = 2
  2. while (a += 1) < 20
  3. if a == 10
  4. break # goes to 'puts a'
  5. end
  6. end
  7. puts a #=> 10

break can also take a parameter which will then be the value that gets returned:

  1. def foo
  2. loop do
  3. break "bar"
  4. end
  5. end
  6. puts foo #=> "bar"