Ruby getting the longest word of a sentence
ruby
Solution
It depends on how you want to split the string. If you are happy with using a single space, than this works:
def longest(source)
arr = source.split(" ")
arr.sort! { |a, b| b.length <=> a.length }
arr[0]
end
Otherwise, use a regular expression to catch whitespace and puntuaction.
Problem
I'm trying to create method named `longest_word` that takes a sentence as an argument and The function will return the longest word of the sentence. My code is: ``` def longest_word(str) words = str.split(' ') longest_str = [] return longest_str.max end ```