I think the best answer is only in the question. Only change
first_non_null_index = (array.compact.empty?) "No 'Non null' value exist" : array.index(array.dup.compact[0]
Consider the following example.
array = [nil, nil, nil, nil, nil]
first_non_null_index = array.index(array.dup.compact[0])
source
share