How to create a file with UTF-8 encoding

J

Jin Lee

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.
 
R

Robert Klemme

2009/9/21 Jin Lee said:
Hi, as known that the default encoding is set to ANSI when creating a
text file. =A0How can I specify the encoding type to UTF-8 other than ANS= I
when creating a text file in RUBY? =A0Thanks a lot.

I've tried the ways found by google, but no available. =A0E.g.
tst =3D 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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
-e:1: warning: encoding options not supported in 1.8: w:UTF-8
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
-e:1: warning: encoding options not supported in 1.8: wb:UTF-8
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
15:04:15 tmp$

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

Jin Lee

Robert said:
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?
 
R

Robert Klemme

2009/9/21 Jin Lee said:
Robert said:
There is an error message: illegal access mode w:UTF-8 (ArgumentError)

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

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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?

Correct. See also

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

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

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
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top