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

Added Enumerable#count_by and tests #1932

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
3 participants

pbyrne commented Jul 1, 2011

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.

pbyrne commented Jul 1, 2011

Feedback on the quality and convention-conformance greatly appreciated.

@pbyrne pbyrne closed this Jul 1, 2011

@pbyrne pbyrne reopened this Jul 1, 2011

Why are there no examples showing how to use the method this comment is for?

Owner

pbyrne replied Jul 6, 2011

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.

Owner

tenderlove commented Jul 6, 2011

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.

@tenderlove tenderlove closed this Jul 6, 2011

pbyrne commented Jul 6, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment