Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

OpenStruct#dig

ostruct.dig(name, *identifiers) → object

Finds and returns the object in nested objects that is specified by name and identifiers. The nested objects may be instances of various classes. See Dig Methods.

Examples:

Example
require "ostruct"
address = OpenStruct.new("city" => "Anytown NC", "zip" => 12345)
person  = OpenStruct.new("name" => "John Smith", "address" => address)
person.dig(:address, "zip") # => 12345
person.dig(:business_address, "zip") # => nil
See also #delete_field #each_pair #as_json #eql? also in 3.4 · 3.3 · 3.1 · 3.0