From: Siep Korteling Date: 2008-04-26T23:20:02+09:00 Subject: Re: All occurances of a character in a string S2 wrote: > S2 wrote: > >> irb(main):006:1>   ret[c] = ret[c].nil? ? d : d << ret[c] >> irb(main):007:1>   ret[c].flatten! > > oh sorry, here are a few things that are not needed: > a = "abbccdaab" > i = 0 > ret = Hash.new > a.scan(/./) {|c| > ret[c] = ret[c].nil? ? [i] : ret[c] << i > i = i + 1 > } > p a > p ret The block form of Hash.new gets rid of the if-statement; like this: str = "abbccdaab" ar = str.split("") result = Hash.new{Array.new} ar.each_with_index do|item,index| result[item] = result[item]<