Search code examples
ruby-on-railsrubyjrubyjrubyonrails

what should be Ruby Safe navigator expression for account["details"]["name"]?


Trying to write with safe navigator :

account["details"]["name"]

below doesn't work with unknown method & error.

account&["details"]&["name"]

using jRuby9, rails-5.2


Solution

  • The safe navigator version would look like this:

    account&.[]("details").&[]("name")
    

    Or you might consider using Hash#dig:

    account.dig("details", "name")