Last Updated: February 25, 2016
·
1.499K
· hannesg

return from block

You can use next to return from a block just like you use return to return from a function.

Example:

def result_of_block
  puts yield
end

result_of_block do
    next "I'm the result"
    "I will never be reached"
end
# outputs "I'm the result"

3 Responses
Add your response

Sometimes break is better. For example if we want to escape from #each.

over 1 year ago ·

@hauleth Indeed! Depends on what you actually want.

over 1 year ago ·

I was looking for that, thanks!

over 1 year ago ·