Skip to content

Instantly share code, notes, and snippets.

@kinopyo
Created July 10, 2012 06:46
Show Gist options
  • Save kinopyo/3081634 to your computer and use it in GitHub Desktop.
Save kinopyo/3081634 to your computer and use it in GitHub Desktop.
Rails: Equivalent of .try() for a hash
foo = { :a => '11', :b => '22' }
foo[:b] # => "22"
# equivalent of .try() for a hash
foo.try(:[], :b) # => "22"
# using variable as key
key = :b
foo.try(:[], key) # => "22"
@kinopyo
Copy link
Author

kinopyo commented Jul 10, 2012

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