[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

UTF-8 in Ruby

Vaibhav Bhandari

4/3/2008 4:05:00 PM

[Note: parts of this message were removed to make it a legal post.]

hi all -

Whats the recommended way to ensure that a string sent over the wire is
UTF-8. I want to do a post to a web-service and the content needs to be
UTF-8.

Whats the best way to do -
"content".encoding('UTF-8')

Thanks in advance.

regards,
Vaibhav

3 Answers

Lionel Bouton

4/3/2008 4:23:00 PM

0

Vaibhav Bhandari wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> hi all -
>
> Whats the recommended way to ensure that a string sent over the wire is
> UTF-8. I want to do a post to a web-service and the content needs to be
> UTF-8.
>
> Whats the best way to do -
> "content".encoding('UTF-8')
>

Hum, it depends on your context. If you don't know anything about the
string and must make a guess, charguess or chardet are good tools.

Note that if you simply want to filter out content which can't be utf-8
the following can be used,

class String
def utf8_ok?
utf8_ok = true
begin
# will raise an error (Iconv::InvalidSequence from memory)
# on invalid utf-8 content
Iconv.iconv(utf-8, utf-8, self)
rescue
utf8_ok = false
end
utf8_ok
end
end

I use a slighty more complex version where I cache an Iconv instance.
This helped solve my UTF-8 problems with one of my databases.

Lionel

Michael Fellinger

4/3/2008 4:29:00 PM

0

On Thu, Apr 3, 2008 at 6:05 PM, Vaibhav Bhandari <vaibhavb@gmail.com> wrote:
> hi all -
>
> Whats the recommended way to ensure that a string sent over the wire is
> UTF-8. I want to do a post to a web-service and the content needs to be
> UTF-8.
>
> Whats the best way to do -
> "content".encoding('UTF-8')
>
> Thanks in advance.
>
> regards,
> Vaibhav

Maybe:

require 'kconv'
"content".toutf8

^ manveru

Jimmy Kofler

5/1/2008 2:03:00 PM

0

> UTF-8 in Ruby
> Posted by Vaibhav Bhandari (Guest) on 03.04.2008 18:05
> hi all -
>
> Whats the recommended way to ensure that a string sent over the wire is
> UTF-8. I want to do a post to a web-service and the content needs to be UTF-8.
>
> Whats the best way to do -
> "content".encoding('UTF-8')
>
> Thanks in advance.
>
> regards,
> Vaibhav


You may also use UTF8REGEX in methods like:

"str".utf8?

"str".clean_utf8

See:

http://snippets.dzone.com/posts...

http://www.w3.org/QA/2008/03/html-ch...


Cheers,

jk


--
Posted via http://www.ruby-....