Ruby dig method
dig method
依照 apidock 的說明
dig(*args) public
Extracts the nested value specified
by the sequence of key objects
by calling dig at each step, returning nil if any intermediate step is nil.
h = { foo: {bar: {baz: 1}}}
h.dig(:foo, :bar, :baz) # 1
h.dig(:foo, :zot, :xyz) # nil
g = { foo: [10, 11, 12] }
g.dig(:foo, 1) # 11
g.dig(:foo, 1, 0) # TypeError: Integer does not have #dig method
g.dig(:foo, :bar) # TypeError: no implicit conversion of Symbol into Integer

留言
張貼留言