0

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

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

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

4

1 回答 1

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"]}
于 2013-04-23T12:54:02.043 回答