Stefano Crocco wrote: > a = [2,4,6,2,1,3,4,6,4,1,2,3,1] > res = Hash.new(0) > a.each do |i| > res[i]+=1 > end > p res > > This creates a hash, which has a default value of 0 (that is, if a key > isn't > included in the hash, the [] method returns 0). Then, there's an > iteration on > all items of the array. For each element, the value of the hash item > corresponding to the array element is increased by one. > If the hash is empty to begin with, when you try to look up the key using res[i] wont it just return 0. I cant see where the hash res is assigned with the unique values from a. Also is it possible to add more keys to a hash - i checked the instance method section in the pick axe but there was nothign like pop or push. -- Posted via http://www.ruby-forum.com/.