Created
July 10, 2012 06:46
-
-
Save kinopyo/3081634 to your computer and use it in GitHub Desktop.
Rails: Equivalent of .try() for a hash
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
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference
http://stackoverflow.com/questions/6224875/equivalent-of-try-for-a-hash