From: Lionel Bouton Date: 2007-09-11T23:18:43+09:00 Subject: Re: Substitution with Hash Robert Klemme wrote: > >> h = {"~" => "~"} >> >> "hmm ~'.split(/ /).map{|c| h[c] || c}.join(' ') >> >> Outputs hmm ~, but obviously doing things like question marks wont work, >> Maybe i'll have to use loops and string#tr >> > > I'd rather not do the split step, IMHO direct replacement will be faster: > If it's all for html entities yes. I'm not sure of what the actual use case is though. > h = {"#126" => "~"} > s.gsub(/&([^;]+);/) {|c| h[c] || "&#{c};"} > > Btw, I believe there are standard classes that do this type of > replacement (entities in HTML documents) - maybe it's in CGI. > The htmlentities gem (more robust than CGI with UTF-8...) is quite good.