Ruby-on-rails – How to find the maximum position in the array?

If I have

ary = [7, 8, 0, 1, nil, 6]

How to find the position of the maximum value in the array? I can do this, but it needs more than one line.

This will return the first in the array Index of the maximum value:

ary = [7, 8, 0, 1, nil, 6, 8]
ary.index(ary. compact.max)
=> 1

If I have

ary = [7 , 8, 0, 1, nil, 6]

How to find the position of the maximum value in the array? I can do this, but it requires more than one line.

This will return the index of the first largest value in the array:

ary = [7, 8, 0, 1, nil, 6, 8]
ary.index(ary.compact.max)
=> 1< /pre>

Leave a Comment

Your email address will not be published.