Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
Created April 15, 2012 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnunemaker/2392836 to your computer and use it in GitHub Desktop.
Save jnunemaker/2392836 to your computer and use it in GitHub Desktop.
fetch as an assertion
# 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!
@jnunemaker
Copy link
Author

Never thought of using fetch to assert a key. I like this idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment