1

I have an array returned by the function Search.all :

=> [#<Search id: 7, name: "ap", presence: true, created_at: "2012-03-16 00:58:42", updated_at: "2012-03-16 00:58:42">, #<Search id: 8, name: "papier", presence: true, created_at: "2012-03-16 01:14:32", updated_at: "2012-03-16 01:14:32">, #<Search id: 9, name: "carton de jus", presence: true, created_at: "2012-03-20 22:28:53", updated_at: "2012-03-20 22:28:53">, #<Search id: 10, name: "carton de jus", presence: true, created_at: "2012-03-20 22:29:01", updated_at: "2012-03-20 22:29:01">, #<Search id: 11, name: "Papier", presence: true, created_at: "2012-03-22 20:43:36", updated_at: "2012-03-22 20:43:36">, #<Search id: 12, name: "Papier", presence: true, created_at: "2012-03-22 20:43:47", updated_at: "2012-03-22 20:43:47">, #<Search id: 13, name: "Salut", presence: false, created_at: "2012-03-24 20:34:49", updated_at: "2012-03-24 20:34:49">, #<Search id: 14, name: "carton", presence: true, created_at: "2012-03-26 19:32:03", updated_at: "2012-03-26 19:32:03">, #<Search id: 15, name: "carton", presence: true, created_at: "2012-03-26 19:32:11", updated_at: "2012-03-26 19:32:11">, #<Search id: 16, name: "carton", presence: true, created_at: "2012-03-26 19:32:15", updated_at: "2012-03-26 19:32:15">, #<Search id: 17, name: "cellulaire", presence: true, created_at: "2012-03-26 19:32:28", updated_at: "2012-03-26 19:32:28">, #<Search id: 18, name: "cellulaire", presence: true, created_at: "2012-03-26 19:32:36", updated_at: "2012-03-26 19:32:36">, #<Search id: 19, name: "montre", presence: false, created_at: "2012-03-29 00:45:26", updated_at: "2012-03-29 00:45:26">, #<Search id: 20, name: "montre", presence: false, created_at: "2012-03-29 00:45:29", updated_at: "2012-03-29 00:45:29">, #<Search id: 21, name: "montres", presence: false, created_at: "2012-03-29 00:45:32", updated_at: "2012-03-29 00:45:32">, #<Search id: 22, name: "montre", presence: false, created_at: "2012-03-29 00:45:35", updated_at: "2012-03-29 00:45:35">]

I want to count the number of occurences of the same elements based on "name". I know how to find the occurences in an array like this one : ['a', 'b', 'a'] with

favoris.inject(Hash.new(0)) { |h,v| h[v] += 1; h }

But how can use that in the previous array?

1
  • 1
    Simple. Replace h[v] with h[v.name] (or however you get the name). Commented Mar 29, 2012 at 0:56

2 Answers 2

6
favoris.inject(Hash.new(0)) { |h,v| h[v.name] += 1; h }

:)

Sign up to request clarification or add additional context in comments.

1 Comment

I needed to wait 8 minutes before I could.
0

I prefer a solution based on each, as you're actually iterating on every element of the array, while producing side effects. That, for how I understand it, is not the reason inject was created.

h = Hash.new(0)
favoris.each { |el| h[el] += 1 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.