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"
Written by Hannes Georg
Related protips
3 Responses
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#