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

60 views Asked by At

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

1

There are 1 answers

1
spickermann On BEST ANSWER

The safe navigator version would look like this:

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

Or you might consider using Hash#dig:

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