I found my mistake is using incorrect reg match, it works after change 
to

s=s.gsub(/#{cnarray[i]}/, x.to_s)

thanks everyone!
-- 
Posted via http://www.ruby-forum.com/.