Ruby: how to know depth of multidemensional array

arrays, multidimensional-array, ruby

Solution

If this is not what you're looking for, it should be a good starting point:

def depth (a)
  return 0 unless a.is_a?(Array)
  return 1 + depth(a[0])
end

> depth(arrA)
=> 3

Please note that this only measures the depth of the first branch.

Problem

This is my problem I have met in my assignment. - Array A has two elements: array B and array C. - Array B has two elements: array D and array E - At some point, array X just contains two elements: string a and string b. I don't know how to determine how deep array A is. For example: ``` arrA = [ [ [1,2] ] ] ``` I have tested by: `A[0][0][0] == nil` which returns `false`. Moreover, `A[0][0]..[0] == nil` always returns `false`. So, I cannot do this way to know how deep array A is.

Original source

Related problems