How to create a file with UTF-8 encoding

Hi, as known that the default encoding is set to ANSI when creating a
text file. How can I specify the encoding type to UTF-8 other than ANSI
when creating a text file in RUBY? Thanks a lot.

I’ve tried the ways found by google, but no available. E.g.
tst = File.new(“abc.txt”,“w:UTF-8”)
There is an error message: illegal access mode w:UTF-8 (ArgumentError)

Please help me if there is another way to work it out, thanks.

2009/9/21 Jin L. [email protected]:

Hi, as known that the default encoding is set to ANSI when creating a
text file. How can I specify the encoding type to UTF-8 other than ANSI
when creating a text file in RUBY? Thanks a lot.

I’ve tried the ways found by google, but no available. E.g.
tst = File.new(“abc.txt”,“w:UTF-8”)
There is an error message: illegal access mode w:UTF-8 (ArgumentError)

You did not mention your Ruby version. I do not see your error:

15:03:23 tmp$ allruby -e ‘File.open(“x”, “w:UTF-8”) {|io|
io.write(“hello”)}’
CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
-e:1: warning: encoding options not supported in 1.8: w:UTF-8

ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]

15:04:07 tmp$ allruby -e ‘File.open(“x”, “wb:UTF-8”) {|io|
io.write(“hello”)}’
CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
-e:1: warning: encoding options not supported in 1.8: wb:UTF-8

ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
15:04:15 tmp$

Cheers

robert

Robert K. wrote:

There is an error message: illegal access mode w:UTF-8 (ArgumentError)

You did not mention your Ruby version. I do not see your error:

========================================
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
15:04:15 tmp$

Cheers

robert

Thanks Robert, the version of mine is 1.8.6 (2007-09-24 patchlevel 111)

It seems that this encoding option is only supported in 1.9?

Robert K. wrote:

2009/9/21 Jin L. [email protected]:

robert

Thanks Robert, the version of mine is 1.8.6 (2007-09-24 patchlevel 111)

It seems that this encoding option is only supported in 1.9?

Correct. See also

http://blog.grayproductions.net/articles/miscellaneous_m17n_details

Cheers

robert

Thanks again, it’s talking about encodings in 1.9.

So how can I get it work in previous versions, such as 1.8.6?

Regards,
Jin

2009/9/21 Jin L. [email protected]:

robert

Thanks Robert, the version of mine is 1.8.6 (2007-09-24 patchlevel 111)

It seems that this encoding option is only supported in 1.9?

Correct. See also

http://blog.grayproductions.net/articles/miscellaneous_m17n_details

Cheers

robert