Longest item in array

Posted by c00lryguy on Stack Overflow See other posts from Stack Overflow or by c00lryguy
Published on 2010-05-07T22:13:36Z Indexed on 2010/05/07 22:18 UTC
Read the original article Hit count: 190

Filed under:

Is there an easier way than below to find the longest item in an array?

arr = [
    [0,1,2],
    [0,1,2,3],
    [0,1,2,3,4],
    [0,1,2,3]
]

longest_row = []
@rows.each { |row| longest_row = row if row.length > longest_row.length }

p longest_row # => [0,1,2,3,4]

© Stack Overflow or respective owner

Related posts about ruby