Break if : break « Statement « Ruby






Break if


block = Proc.new do |x|
  puts x
  break if x == 3
  puts x + 2
end

block.call(5)
# 5
# 7

block.call(3)
# 3
# LocalJumpError: break from proc-closure

 








Related examples in the same category

1.break ends the immediately enclosing loop.
2.Break out of a block
3.Stopping an Iteration
4.Read till 'quit'