Safely assign value to nested hash using Hash#dig or Lonely operator(&.) : <pre><code>h = {
  data: {
    user: {
      value: "John Doe" 
    }
  }
}
</code></pre>

<p>To assign value to the nested hash, we can use</p>

<pre><code>h[:data][:user][:value] = "Bob"
</code></pre>

<p>However if any part in the middle is missing, it will cause error.</p>

<p>Something like </p>

<pre><code>h.dig(:data, :user, :value) = "Bob"
</code></pre>

<p>won't work, since there's no <code>Hash#dig=</code> available yet.</p>

<p>To safely assign value, we can do</p>

<pre><code>h.dig(:data, :user)&amp;.[]=(:value, "Bob")    # or equivalently
h.dig(:data, :user)&amp;.store(:value, "Bob")
</code></pre>

<p>But is there better way to do that? </p>
