从字符串中提取的最大和最小单词

aru*_*roy 1 ruby

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

{2=>["So", "to", "do"], 8=>["Frederik", "tomorrow"]}
Run Code Online (Sandbox Code Playgroud)

那么最简单的方法是什么呢?

Aru*_*hit 8

试试以下内容:

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"]}
Run Code Online (Sandbox Code Playgroud)