From 073154d56760a62eace06b75c1c8f196d6c71ae0 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 17 Nov 2015 05:36:03 +0000 Subject: OpenStruct#dig * lib/ostruct.rb (dig): Implement OpenStruct#dig [Feature #11688] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52611 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/ostruct.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib/ostruct.rb') diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 63146075be..0e13f40692 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -214,6 +214,24 @@ class OpenStruct modifiable[new_ostruct_member(name)] = value 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. -- cgit v1.2.3