Reputation: 601
I do have an string as "Frederik will not come office tomorrow.So please you have to do his tasks".
I want smallest and maximum length words as a hash as below:
{2=>["So", "to", "do"], 8=>["Frederik", "tomorrow"]}
So what would be shortest approach to do that?
Upvotes: 0
Views: 124
Reputation: 118271
Try the below:
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"]}
Upvotes: 8