Reputation: 5603
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?
Upvotes: 0
Views: 180
Reputation: 5603
I found an answer within grep
.
arr.grep(/^Passive/).each do |element|
...
end
This will suffice for my purpose.
Upvotes: 0
Reputation: 168091
["Passive: 8", "Passive: 9", "Neutral: 3"].grep(/Passive/).count
# => 2
Upvotes: 4