GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
Just as it says on the tin. Added Enumerable#count_by which performs a group_by and returns the keys and count of matching elements rather than the array of matching elements. Includes failing test, the method itself, and RDoc for the method.
Added Enumerable#count_by and tests
Feedback on the quality and convention-conformance greatly appreciated.
Why are there no examples showing how to use the method this comment is for?
I'm not sure I understand the question. I modeled the two examples in the commit off the Enumerable#group_by examples. (I realize that I inadvertently indicated parents.group_by rather than parents.count_by, though, and am fixing that. Is that what you mean?)
Yep, that's what I meant.
fixed method name in rdoc
I don't think this should go in active record. The use case seems too specific.
You can just use group_by. The values of the hash are lists, and each list has a length associated with it. Any time you need to know the list length, just ask for it.
I've used count_by in my own projects extensively while logging and debugging in the console, since it's a pain to convert the group_by hash by hand into counts. Before writing a gem for it, I thought I'd take a shot to add it to the other Enumerable additions that ActiveSupport provides.
Thanks for taking a look.