Ruby getting the longest word of a sentence

前端 未结 7 964
时光说笑
时光说笑 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 11:52

    If you truly want to do it in the Ruby way it would be:

    def longest(sentence)
    
         sentence.split(' ').sort! { |a, b| b.length <=> a.length }[0]
    
    end
    

提交回复
热议问题