If you're using this pattern with resources which need to be cleaned up, don't forget to ensure! :)
ensure
def get_blender blender = take_blender_out_of_cabinet() blender.plug_in() yield blender ensure if blender clean_up blender put_away blender end end
Without ensure, your cleanup can be bypassed with
raise exception
throw tag
break
return
Using ensure get's you back to what you're used to with defer.
defer
Thanks for sharing that Ruby idiom, I had not heard about that keyword before!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If you're using this pattern with resources which need to be cleaned up, don't forget to
ensure
! :)Without
ensure
, your cleanup can be bypassed withraise exception
anywhere laterthrow tag
anywhere down the stackbreak
exits the block it's inreturn
exits the method it's inUsing
ensure
get's you back to what you're used to withdefer
.Thanks for sharing that Ruby idiom, I had not heard about that keyword before!