Refactor code with hash, remove iteration on string and use gsub instead