Here's one way to find the shortest word in a string in Ruby:
main.rb176 chars9 linesThis code defines a method shortest_word that takes a string as input. It first splits the string into an array of words using the split method, with the separator being a space. It then initializes the shortest variable as the first word in the array.
The method then iterates over each word in the array using each and compares the length of the current word with the length of shortest. If word is shorter than shortest, it becomes the new value of shortest.
Finally, the method returns shortest, which is the shortest word in the input string.
Example usage:
main.rb141 chars3 lines
gistlibby LogSnag