Skip to content

Instantly share code, notes, and snippets.

@awead
Last active May 29, 2019 12:54
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 awead/89cdb2645d096b34a07f4cb1c2ece8fd to your computer and use it in GitHub Desktop.
Save awead/89cdb2645d096b34a07f4cb1c2ece8fd to your computer and use it in GitHub Desktop.
Using fetch
> {}.fetch("i-dont-exist") # Raises error for a required key
KeyError: key not found: "i-dont-exist"
from (pry):2:in `fetch'
> {}.fetch("with-a-default", "default-value") # Provide a default value as the second argument
=> "default-value"
> {}.fetch("something-special") { puts "do something special here" } # Yields to a block for custom stuff
do something special here
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment