Ruby | Hash dig() function
Hash#dig() is a Hash class method which finds the nested value which is specified by the sequence of the key object by calling dig at each step.
Syntax: Hash.dig()
Parameter: Hash values
Return: nested value which is specified by the sequence of the key object by calling dig at each step. otherwise nil
Example #1 :
# Ruby code for Hash.dig() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, b:200} # declaring Hash value c = {a:100} # Dig Value puts "Hash a dig form : #{a.dig(:a)}\n\n" puts "Hash b dig form : #{b.dig(:c)}\n\n" puts "Hash c dig form : #{c.dig(:a)}\n\n" |
chevron_right
filter_none
Output :
Hash a dig form : 100 Hash b dig form : 300 Hash c dig form : 100
Example #2 :
# Ruby code for Hash.dig() method # declaring Hash value a = { "a" => 100, "b" => 200 } # declaring Hash value b = {"a" => 100} # declaring Hash value c = {"a" => 100, "c" => 300, "b" => 200} # Dig Value puts "Hash a dig form : #{a.dig("a")}\n\n" puts "Hash b dig form : #{b.dig("a")}\n\n" puts "Hash c dig form : #{c.dig("b")}\n\n" |
chevron_right
filter_none
Output :
Hash a dig form : 100 Hash b dig form : 100 Hash c dig form : 200
Recommended Posts:
- Ruby | Hash key() function
- Ruby | Hash any?() function
- Ruby | Hash each() function
- Ruby | Hash eql? function
- Ruby | Hash key?() function
- Ruby | Hash assoc() function
- Ruby | Range hash() function
- Ruby | Hash keys() function
- Ruby | Hash clear() function
- Ruby | Hash merge function
- Ruby | Hash merge! function
- Ruby | Hash compact!() function
- Ruby | Hash compact() function
- Ruby | Hash has_key?() function
- Ruby | Hash flatten() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



