diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-11-17 05:36:03 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-11-17 05:36:03 +0000 |
commit | 482530680c18ea2e44c3300c6f323fabc3bd55f7 (patch) | |
tree | b7df8edab6d18d9c8b26a611619a31b12f8a45d7 /lib/ostruct.rb | |
parent | 66e2139b1a668681e63422cff57ddc2c36859b94 (diff) |
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
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. # |