Skip to content

Instantly share code, notes, and snippets.

@jkaihsu
Created March 7, 2013 08:17
Show Gist options
  • Save jkaihsu/5106402 to your computer and use it in GitHub Desktop.
Save jkaihsu/5106402 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment