Created
April 15, 2012 13:38
-
-
Save jnunemaker/2392836 to your computer and use it in GitHub Desktop.
fetch as an assertion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from http://avdi.org/talks/confident-code-rubymidwest-2011/confident-code.html | |
opt = {}.fetch(:required_option) | |
# IndexError: key not found | |
opt = {}.fetch(:required_option) do | |
raise ArgumentError, "Missing option!" | |
end | |
# ArgumentError: Missing option! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Never thought of using fetch to assert a key. I like this idea.