From: Brian Candler Date: 2009-09-02T21:15:02+09:00 Subject: Re: Ruby1.9: Encoding problems (how to use #force_encoding ?) I単aki Baz Castillo wrote: > Hi, I'm using geo_location Ruby gem which returns to me a hash with the > given > IP geolocation. Lots of gems are not ruby-1.9 compatible. You should probably report problems to the author, ideally with a patch which fixes it, and a test case which reproduces it. > I use Ruby1.9 and UTF-8 works fine, but in this case, when the "city" > has > "strange" symbols the the gem gives the string encoded in ASCII-8BIT. All data read from a socket is tagged as ASCII-8BIT by default. That's probably what's happening in the library you're using. > I need to send this string to a server which mandates UTF-8 usage so > sending > it as it's fails. That doesn't make much sense. A string, when it hits a socket, is just a stream of bytes. So you should be sending the same stream of bytes as you receive. > I've tryed to convert the encoding but received an error: > > result.encode "UTF-8" > => `encode': "\xF3" from ASCII-8BIT to UTF-8 > (Encoding::UndefinedConversionError) That's correct. Transcoding tries to *transcode* (replace characters one at a time), and these high characters in ASCII-8BIT have no Unicode equivalents. > I've also tryed with force_encoding: > result.force_encoding "UTF-8" > > and then, the "result" string is converted to UTF-8 (I've checked > result.encoding) It's not converted, it's just tagged as being a string of UTF-8 characters, which it sounds like it is. > but it's also not valid for the server Again, doesn't mean much without seeing the code which is trying to submit this to the server. > I need all of this just for a simple demo, so it owuld be valid for me > just to > delete the non valid UTF-8 chars from the result string, but I don't > know > how to do it. str.force_encoding("ASCII-8BIT") # if not already str.gsub!(/[^\x20-\x7e]/,'') -- Posted via http://www.ruby-forum.com/.