How can I return something early from a block?

Posted by ryeguy on Stack Overflow See other posts from Stack Overflow or by ryeguy
Published on 2010-03-25T17:51:37Z Indexed on 2010/03/25 17:53 UTC
Read the original article Hit count: 200

Filed under:

If I wanted to do something like this:

collection.each do |i|
   return nil if i == 3

   ..many lines of code here..
end

How would I get that effect? I know I could just wrap everything inside the block in a big if statement, but I'd like to avoid the nesting if possible.

© Stack Overflow or respective owner

Related posts about ruby