>["nation", "nation", "nation"] => 3
["americans", "oath", "oath", "generation", "americans", "crisis", "crisis", "generation"] => 2,
所以现在我得到的所有这些键都等于相同的值(见上文),我想将它们拆分,以便每个单词=一个值,如下所示。
"nation" => 3
"oath" => 2
"crisis" => 2
Enumerable#tally
方法:
[
"americans", "oath", "oath", "generation", "americans", "crisis", "crisis", "generation"
].tally
#=> { "americans" => 2, "oath" => 2, "generation" => 2, "crisis" =>2 }