Counting and Tallying
tally counts items and count counts matches. each_with_object builds results.
Example
p %w[a b a c a].tally
puts [1, 2, 3, 4].count(&:even?)
p (1..5).each_with_object([]) { |i, acc| acc << i * i }Result
{"a"=>3, "b"=>1, "c"=>1}
2
[1, 4, 9, 16, 25]Exercise
Count occurrences.
a.Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.