All posts

How to break out of a block in Ruby


When first learning to program in Ruby it can be difficult to find an answer to the question; “I need to stop iterating this collection once I find the desired element”. I think a lot of people try to return from the block as a first attempt, but that doesn’t return from the block, it returns from the entire method, which in this case isn’t the desired behavior.

So when you need to exit a block early you can use break to handle this, it will exit the block and return control back the method the block was running in.

This is different from if you want to just skip the current iteration of the block, in that event you would want to use the next keyword.

[1,2,3,4].each{|int| break if int == 3}

Or if you want to assign a value before breaking:

[1,2,3,4].each do |int| 
  if int == 3
    @desired_number = int


I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).