I have used this before when I needed to find files which have the same
content. Basically I did this: (Yes, I used exactly the same name and
almost the same logic as you. :))
module Enumerable
def group_by
result = Hash.new { |h, k| h[k] = Array.new }
self.each do |item|
group = yield(item)
result[group] << item
end
return result
end
end
And later:
files.group_by { |file| hash_file(file) }
So, yes, I think that this would make a great extension to the standard
Ruby.
I say it is. But if you just want something you can download and use
anywhere without reimplementing, ‘extensions’ implements
Enumerable#partition_by. Different name, same thing.
Cheers,
Gavin
···
On Wednesday, May 26, 2004, 3:16:27 AM, Michael wrote:
For example:
%w(This is a list of words).group_by {|word| word.size}