Last active
May 29, 2019 12:54
-
-
Save awead/89cdb2645d096b34a07f4cb1c2ece8fd to your computer and use it in GitHub Desktop.
Using fetch
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
> {}.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