Greg W. wrote:
I found a suggestion that says to use return, but this generates a
colors = [‘red’, ‘yellow’, ‘green’, ‘blue’]
colors.each do |color|
return color if color == ‘yellow’
My actual code is a little different but I am assuming whatever
technique solves the above would solve what I need as well.
Seems like a simple syntax need, but I’m not finding a conclusive
solution. Probably right in front of me, so sorry if it’s major
Your question is not what you really want to ask about.
You don’t want to exit the block. You want to exit the loop that is
inside the each method. And you break out from loops using break.
Just exiting the block would be useless, because you would still be
inside the loop, and the each would just go on to the next iteration. In
fact, you exit the block each time control gets to the end of the block
normally, but then the block is called again in the next iteration.