What’s the most concise way to determine if @hash[:key1][:key2] is defined, that does not throw an error if @hash or @hash[:key1] are nil?
defined?(@hash[:key1][:key2]) returns True if @hash[:key1] exists (it does not determine whether :key2 is defined)
When using ActiveSupport (Rails) or Backports, you can use
try:You could even handle
@hashbeingnil:If you want
@hashto always return a hash for a missing key:You could also define this recursive:
But to answer your question: