如何用ruby统计数组或哈希中不同元素的个数?得到不同数据的值: %w(a b c a c d).uniq 得到各个元素出现的个数: count_hash = {} %w(a b c a c d).each do |item| key = item.to_sym if count = count_hash[key] count_hash[key] = count 1 else count_hash[key] = 1 end end