From: Adam Shelly Date: 2008-01-23T06:18:19+09:00 Subject: Re: Question about hsh.each_pair > I was wondering if the hash.each_pair function messes with the order of > my hash? > It's not the each_pair method, it's the hash itself. Hashes are unordered - or more specifically, ordered by the hash value of the key, which is unrelated to the order in which you added items. > I am using it to parse some text and replace html entities and > apparently it changes the order of my hash before iterating each > pairs... > Now obviously, the substitution needs to be done in the correct order > (i.e. & needs to be parsed before <...) > > Any ideas as to why the hash seems to be re-ordered before the do block > is executed, and how I can prevent this?? You could try a nested array instead rep = [[ "&", """], ["<","<"] > ...] rep.each do |key, value| text.gsub(key, value) end or, maybe more simply: require 'CGI' CGI.escapeHTML(text)