我有一个字符串,因为"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)
那么最简单的方法是什么呢?
试试以下内容:
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)