Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created January 3, 2013 06:37
Show Gist options
  • Select an option

  • Save kmandreza/4441292 to your computer and use it in GitHub Desktop.

Select an option

Save kmandreza/4441292 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.
# longest_string is a method that takes an array of strings as its input
# and returns the longest string
#
# +array+ is an array of strings
# longest_string(array) should return the longest string in +array+
#
# If +array+ is empty the method should return nil
def longest_string(array)
longest = nil
array.each do |x|
if longest.nil? || longest.length < x.length
longest = x
end
end
longest
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment