Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save kmandreza/4441287 to your computer and use it in GitHub Desktop.
Write a method shortest_string which takes as its input an Array of Strings and returns the shortest String in the Array. For example: # 'cat' is 3 characters long shortest_string(['cat', 'zzzzzzz', 'apples']) # => "cat" # '' is 0 characters long, but it's the only string shortest_string(['']) # => '' shortest_string([]) # => nil If the input Ar…
# shortest_string is a method that takes an array of strings as its input
# and returns the shortest string
#
# +array+ is an array of strings
# shortest_string(array) should return the shortest string in +array+
#
# If +array+ is empty the method should return nil
def shortest_string(array)
shortest = nil
array.each do |x|
if shortest.nil? || shortest.length > x.length
shortest = x
end
end
shortest
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment