Open In App
Related Articles

Ruby | Hash dig() function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
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"

                    
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"

                    
Output :
Hash a dig form : 100

Hash b dig form : 100

Hash c dig form : 200


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads