Ruby getting the longest word of a sentence

前端 未结 7 920
时光说笑
时光说笑 2021-01-12 11:27

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 c

7条回答
  •  长发绾君心
    2021-01-12 12:08

    The shortest way is to use Enumerable's max_by:

    def longest(string)
      string.split(" ").max_by(&:length)
    end
    

提交回复
热议问题