Matt Elhotiby
Matt Elhotiby

Reputation: 44066

Ruby: How to group a Ruby array?

I have a Ruby array

> list = Request.find_all_by_artist("Metallica").map(&:song)
=> ["Nothing else Matters", "Enter sandman", "Enter Sandman", "Master of Puppets", "Master of Puppets", "Master of Puppets"]

and I want a list with the counts like this:

{"Nothing Else Matters" => 1,
 "Enter Sandman" => 2,
 "Master of Puppets" => 3}

So ideally I want a hash that will give me the count and notice how I have Enter Sandman and enter sandman so I need it case insensitive. I am pretty sure I can loop through it but is there a cleaner way?

Upvotes: 57

Views: 71406

Answers (7)

Amith T G
Amith T G

Reputation: 1

list = ["Nothing else Matters", "Enter sandman", "Enter Sandman", "Master of Puppets", "Master of Puppets", "Master of Puppets"]

list.each_with_object(Hash.new(0)){|k,v| v[k.downcase] += 1}

Upvotes: 0

BrunoF
BrunoF

Reputation: 3523

As of Ruby 2.7, you can use Enumerable#tally.

list.tally
# => {"Nothing else Matters"=>1, "Enter sandman"=>1, "Enter Sandman"=>1, "Master of Puppets"=>3}

Upvotes: 15

ray
ray

Reputation: 5552

Late but clean answer I have,

l = list.group_by(&:titleize)
l.merge(l) { |k,v| l[k] = v.count }

Note: If we do want unique keys i.e. without titleize, then replace it with itself

Upvotes: 0

sepp2k
sepp2k

Reputation: 370112

list.group_by(&:capitalize).map {|k,v| [k, v.length]}
#=> [["Master of puppets", 3], ["Enter sandman", 2], ["Nothing else matters", 1]]

The group by creates a hash from the capitalized version of an album name to an array containing all the strings in list that match it (e.g. "Enter sandman" => ["Enter Sandman", "Enter sandman"]). The map then replaces each array with its length, so you get e.g. ["Enter sandman", 2] for "Enter sandman".

If you need the result to be a hash, you can call to_h on the result or wrap a Hash[ ] around it.

Upvotes: 100

Harish Shetty
Harish Shetty

Reputation: 64363

Grouping and sorting of a data set of unknown size in Ruby should be a choice of last resort. This is a chore best left to DB. Typically problems like yours is solved using a combination of COUNT, GROUP BY, HAVING and ORDER BY clauses. Fortunately, rails provides a count method for such use cases.

song_counts= Request.count(
              :select => "LOWER(song) AS song"
              :group => :song, :order=> :song,
              :conditions => {:artist => "Metallica"})

song_counts.each do |song, count|
  p "#{song.titleize} : #{count}"
end

Upvotes: 5

ghostdog74
ghostdog74

Reputation: 342333

list.inject(Hash.new(0)){|h,k| k.downcase!; h[k.capitalize] += 1;h}

Upvotes: 11

glenn jackman
glenn jackman

Reputation: 246774

Another take:

h = Hash.new {|hash, key| hash[key] = 0}
list.each {|song| h[song.downcase] += 1}
p h  # => {"nothing else matters"=>1, "enter sandman"=>2, "master of puppets"=>3}

As I commented, you might prefer titlecase

Upvotes: 9

Related Questions