VOTE_ARY = [123, 4, 56, 78] N = 100 class Hash def gen_vote(ary) key = 'a' ary.each{|x| self[key.succ!] = x} end def gen_vote_list keys.each{|key| 2.upto(N){|i| self["#{key}#{i}"] = self[key].quo(i)} } end end votes, counter = Hash.new, Hash.new votes.gen_vote(VOTE_ARY) votes.gen_vote_list votes.keys.sort{|a, b| -1*(votes[a] <=> votes[b])}. map{|x| x.match(/^([a-zA-Z]+)/)[0]}[0..N-1]. each{|key| eval("counter[key]#{counter.has_key?(key) ? '+' : ''}=1")} p counter.values