arun_roy
arun_roy

Reputation: 601

Maximum and minimum word extracted from a string

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

Answers (1)

Arup Rakshit
Arup Rakshit

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

Related Questions