jkaihsu
3/7/2013 - 8:17 AM

Write a method longest_string which takes as its input an Array of Strings and returns the longest String in the Array. For example: #

Write a method longest_string which takes as its input an Array of Strings and returns the longest String in the Array.

For example:

'zzzzzzz' is 7 characters long

longest_string(['cat', 'zzzzzzz', 'apples']) # => "zzzzzzz" If the input Array is empty longest_string should return nil.

def longest_string(array)

  if array.empty?
    nil
  else 
    long_string = array.group_by(&:size).max.last
    long_string[0]  
    end
end