Мы можем использовать метод Enumerable # tally , новый в v2.7, для этого:
data.map { |h| h.keys.first }.
tally.
sort_by { |_,count| -count }.
each { |k,v| puts "#{k} #{v} unique views" }
отображает:
help_page/1 5 unique views
home 3 unique views
contact 2 unique views
about 1 unique views
index 1 unique views
about/2 1 unique views
шаги следующие.
a = data.map { |h| h.keys.first }
#=> ["help_page/1", "contact", "home", "about/2", "help_page/1",
# "index", "help_page/1", "about", "help_page/1", "home",
# "help_page/1", "home", "contact"]
b = a.tally
#=> {"help_page/1"=>5, "contact"=>2, "home"=>3, "about/2"=>1,
# "index"=>1, "about"=>1}
c = b.sort_by { |_,count| -count }
#=> [["help_page/1", 5], ["home", 3], ["contact", 2], ["about", 1],
# ["index", 1], ["about/2", 1]]
c.each { |k,v| puts "#{k} #{v} unique views" }
#=> <displays the results shown above>