[newbie] specifying file encoding

Y

Yvon Thoraval

i have a very simple script in ISO-8859-1 :
#!/usr/bin/ruby

w = "émilion"
fo = File.open("capitalize.txt", File::RDWR|File::CREAT,0600)
fo.puts w.capitalize
fo.close

resulting in :
Èmilion E grave instead of Eacute

But the prob arroses because the output file "capitalize.txt" is in
MacOS Roman...

How could we secify the encoding of I/O files ???

In fact, the prob is worth than i thought if i change the encoding of
the file for the script to MacOS Roman

the w.capitalize
is written as :
émilion
that's to say NO capitalisation on accentuated chars ???
 
E

Emmanuel Touzery

How could we secify the encoding of I/O files ???

a string is considered by ruby as encoded as <insert your locale here>. If you
want to treat strings from other encodings, you can convert encoding using
"iconv" (bundled with ruby 1.8); in your case, you can use iconv to convert
the string to your local encoding (iso-8859-1) and then capitalize will work
fine.

emmanuel

PS: better support for this is planned for ruby 2.0
 
E

Emmanuel Touzery

Emmanuel said:
a string is considered by ruby as encoded as <insert your locale here>. If you
want to treat strings from other encodings, you can convert encoding using
"iconv" (bundled with ruby 1.8); in your case, you can use iconv to convert
the string to your local encoding (iso-8859-1) and then capitalize will work
fine.
which made me wonder: how do I find out what is the current local
encoding for the computer in a portable (unix/windows) way?

emmanuel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top