Ruby getting the longest word of a sentence

前端 未结 7 824
时光说笑
时光说笑 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
    
    0 讨论(0)
提交回复
热议问题