diff options
Diffstat (limited to 'lib/ostruct.rb')
-rw-r--r-- | lib/ostruct.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 63146075be..0e13f40692 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -215,6 +215,24 @@ class OpenStruct end # + # Retrieves the value object corresponding to the each +name+ + # objects repeatedly. + # + # 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 + # + def dig(name, *names) + begin + name = name.to_sym + rescue NoMethodError + return + end + @table.dig(name, *names) + end + + # # Remove the named field from the object. Returns the value that the field # contained if it was defined. # |