我有一个字符串作为"Frederik will not come office tomorrow.So please you have to do his tasks".我希望最小和最大长度的单词作为散列如下:

{2=>["So", "to", "do"], 8=>["Frederik", "tomorrow"]}

那么,最短的方法是什么呢?

最佳答案

请尝试以下操作:

w = "Frederik will not come office tomorrow.So please you have to do his tasks"
p Hash[w.scan(/\w+/).group_by(&:length).minmax]

#=>{2=>["So", "to", "do"], 8=>["Frederik", "tomorrow"]}

关于ruby - 从字符串中提取的最大词和最小词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16169838/

10-11 16:21