DEV Community

matt swanson
matt swanson

Posted on • Originally published at on


Quickly explore your data with `uniq` and `tally`

A common question you may want to answer on user-input data is: what values have been entered and how many times is each one used?

Maybe you have a list of dropdown options and you want to investigate removing a rare-used option.

Ruby has two handy methods that I reach for often: uniq and tally.


The uniq method operates on an enumerable and compresses your data down to unique values.
=> ["Confirmed w/o Outreach",
 "Awaiting Outreach",
 "No Response Expected",
 "Awaiting Reply"]
Enter fullscreen mode Exit fullscreen mode

While most developers are familiar with uniq, the tally method is one of the best kept secrets in Ruby. The tally method takes an enumerable of values and returns a hash where the keys are unique values and the values are the number of times the value appeared in the list.
=> {"Confirmed w/o Outreach"=>106,
 "Awaiting Outreach"=>28,
 "No Response Expected"=>10,
 "Awaiting Reply"=>8}
Enter fullscreen mode Exit fullscreen mode

These two methods are great to have in your toolbox to quickly explore your data in a Rails console.

Additional Resources

Ruby API: Enumerable#uniq

Ruby API: Enumerable#tally

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git