Ruby | Array dig() operation

Array#dig() : dig() is a Array class method which extracts the specific element out of the high dimension sequences.

Syntax: Array.dig()

Parameter: element position.



Return: element from a specific location in sequence, returning nil if any intermediate step is nil.

Code #1 : Example for dig() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for dig() method
  
# declaring array
a = [18, 22, 33, 23, 5, 6]
  
# declaring array
b = [[1, 4], [ 1, 1, 88, 9]]
  
# dig
puts "dig : #{a.dig(1)}\n\n"
  
# dig
puts "dig : #{b.dig(0, 1)}\n\n"
  
# dig
puts "dig : #{b.dig(1, 2)}\n\n"

chevron_right


Output :

dig : 22

dig : 4

dig : 88

Code #2 : Example for dig() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for dig() method
  
# declaring array
a = [["abc"], 
    ["geeks", "dog"], 
    ["1", "2"]]
  
# declaring array
b = [["cow"], ["1", "dog"]]
  
# dig
puts "dig : #{a.dig(2, 1)}\n\n"
  
# dig
puts "dig : #{a.dig(1, 1)}\n\n"
  
# dig
puts "dig : #{b.dig(1, 0)}\n\n"

chevron_right


Output :

dig : 2

dig : dog

dig : 1



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.