Longest-word.rb Problem - Question is unclear to me

Write a method that takes in a string. Return the longest word in

the string. You may assume that the string contains only letters and


You may use the String split method to aid you in your quest.

Difficulty: easy.

def longest_word(sentence)

These are tests to check that your code is working. After writing

your solution, they should all print true.

'longest_word(“short longest”) == “longest”: ’ +
(longest_word(‘short longest’) == ‘longest’).to_s
'longest_word(“one”) == “one”: ’ +
(longest_word(‘one’) == ‘one’).to_s
'longest_word(“abc def abcde”) == “abcde”: ’ +
(longest_word(‘abc def abcde’) == ‘abcde’).to_s

I feel like this question is not clear. Is it asking for the programmer
to create a method that takes a sentence and create the longest word out
of all the possible words in the dictionary?

Take a string

“the longest word”

and split it into individual words


Count the number of letters

3, 7, 4

and return the word with the highest count


Words are separated by spaces.

A simple solution with built-in methods could be:

sentence.split(?\ ).max_by(&:size)

I was reading your topic, and understood that you getting these
exercises from some book. Could you tell me what kind book it is? And
recomend any other you are learning from. Thanks

P.s. tried to solve it too.
def longest_word(sentence)
sentence = “The longest word”
sentence.split.each do |x| x.length end

Don’t really know the command what to do next.

The return value from “x.length” isn’t going anywhere. You’ll need to
capture the result.
Using #max_by the way that Dansei did is probably the most elegant