Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
def group_by_owners(files)
   better_hash = Hash.new { |hash, key| hash[key] = [] }              files.each_with_object(better_hash) {|(k, v), hash| hash[v] << k}
 end

group_by_owners({'Input.txt' => 'Randy', 'Code.py' => 'Stan', 'Output.txt' => 'Randy'}) #=> {"Randy"=>["Input.txt", "Output.txt"], "Stan"=>["Code.py"]}]


What I have tried:

Tried understanding it on my own and also tried stack overflow no answers
Posted
Updated 5-Oct-21 5:42am
Comments
Richard MacCutchan 5-Oct-21 11:58am    
Get a book on Ruby; it will be much faster.

1 solution

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
Comments
Member 11164169 5-Oct-21 11:44am    
Sry man didn't know that.. I thought it was a simple program i just need to know what this block does better_hash = Hash.new { |hash, key| hash[key] = [] } files.each_with_object(better_hash) {|(k, v), hash| hash[v] << k}

I can sort the other things out like what does this do : |hash, key| hash[key] = [] }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900