I have an array:
["Passive: 8", "Passive: 9", "Neutral: 3"]
Now I need to count the number of times Passive is seen. How can I count the number of occurrences a substring is seen within strings inside of an array?
["Passive: 8", "Passive: 9", "Neutral: 3"].grep(/Passive/).count
# => 2
I found an answer within grep.
arr.grep(/^Passive/).each do |element|
...
end
This will suffice for my purpose.
element and add one each time. However, sawa's answer is much more efficient.