From: "Brian Schröder" Date: 2005-08-15T16:49:41+09:00 Subject: Re: utf8 string with reverse question On 15/08/05, Mitch Tishmack wrote: > Hi everyone, long time lurker here. Even longer Ruby user. > > I have a minor problem with a utf8 string. > > In short I see this behavior: > > "Stuhlu".sub(/u/,'�') > => "St�hlu" > "Stuhlu".reverse.sub(/u/,'�').reverse > => "Stuhl\274\303" It seems like reverse is acting on the string as a byte-array. That means you are reversing the two byte character � = '\303\274' into the non-character '\274\303' when reversing the string '�hlutS' Are you trying to build a german -> pig-t�rkisch translator ;) regards, Brian > "Stuhlu".reverse.sub(/u/,'�').split(//).reverse.join > => "Stuhl�" > > The general goal is to sub the final "u" in that word with an > umlauted version and not the first. I started irb with -Ku so that I > get utf8 support in all things ruby. But the behavior of reverse on > the substituted string is really baffling me. > > Does anyone know the reason for the weirdness of reverse after the > sub? The last version was a hack to get things to just work. Am I > mising a Regexp option that would make the final match work? I don't > normally look for a final match to substitute on. And reverse seemed > the most logical choice for a solution. > > Any help would be appreciated! > > Thanks, > Mitch > -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/