Skip to content

Instantly share code, notes, and snippets.

@riethmayer
Created January 20, 2010 19:36
Show Gist options
  • Save riethmayer/282152 to your computer and use it in GitHub Desktop.
Save riethmayer/282152 to your computer and use it in GitHub Desktop.
better use fetch
{}[:width] # => nil
{}[:width] || 40 # => 40
{:width => nil}[:width] || 40 # => 40
{:width => false}[:width] || 40 # => 40
{:width => 29}[:width] || 40 # => 29
{}.fetch(:width) # =>
# ~> -:7:in `fetch': key not found (IndexError)
{}.fetch(:width) { 40 } # => 40
{:width => nil}.fetch(:width) {40} # => nil
{:width => false}.fetch(:width) {40} # => false
{:width => 29}.fetch(:width) {40} # => 29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment