From: Nikolai Weibull Date: 2003-08-13T23:24:09+09:00 Subject: Re: UTF-8 question * Mark J. Reed [Aug, 13 2003 15:50]: > > The problem is that Ruby Strings are still logically treated as > collections of bytes rather than collections of characters. So > String#tr only works for characters whose representation takes up a > single byte - which, in the case of UTF-8, means only the 7-bit > ASCII characters. > Yeah, that's what I guessed. > > To avoid the unightly gsub chaining, you can write your own tr-like > method that takes an array of regexes and a corresponding array > of substitutions: > It should probably be: > class String > def trsub!(res, subs) last = subs[-1] || '' > res.each_with_index do > |re, i| > self.gsub!(re, subs[i] || '') self.gsub!(re, subs[i] || last) > end > end > end > > s.trsub!(%w<� � � � � � � � �>, %w) > Yeah, i've thought of this as well. However, it feels like it will be rather slow. OK, given the input strings are rather short ;-), so I guess it will do. Thanks, nikolai -- ::: name: Nikolai Weibull :: aliases: pcp / lone-star ::: ::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden ::: ::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,php,war3 ::: main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}