Ruby 1.9 - US-ASCII vs UTF-8

P

Petri Kivikangas

So I have source files that contain unicode chars. Do I really have to
put "# encoding: utf-8" at the beginning of every one of these files, or
can I somehow make ruby treat all files by default as utf-8? If I can,
does the change of default encoding from US-ASCII to UTF-8 make my
programs much slower?
 
G

Gregory Brown

So I have source files that contain unicode chars. Do I really have to
put "# encoding: utf-8" at the beginning of every one of these files, or
can I somehow make ruby treat all files by default as utf-8? If I can,
does the change of default encoding from US-ASCII to UTF-8 make my
programs much slower?

No, you can't override the source encoding. You can use flags to set
the default internal / external encodings via locale (or flag), and
ruby -e will use your locale as the source encoding, but last I
checked, you need the magic comments to determine source encoding.

The main reason here is for portability. Otherwise, you could easily
(by accident) write Ruby programs that only run in your environment.

-greg
 
G

Gregory Brown

No, you can't override the source encoding. =A0You can use flags to set
the default internal / external encodings via locale (or flag), and
ruby -e will use your locale as the source encoding, but last I
checked, you need the magic comments to determine source encoding.

Sorry, while the above statement is generally true, I forgot about the
special case for UTF-8. To preserve backwards compatibility:

sandal:~ $ cat x.rb
p __ENCODING__
sandal:~ $ ruby19 -Ku x.rb
#<Encoding:UTF-8>

But I imagine this isn't the recommended approach :)
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top