# Ruby 1.9 - US-ASCII vs UTF-8

**URL:** <https://www.ruby-forum.com/t/ruby-1-9-us-ascii-vs-utf-8/180391>\
**Category:** Ruby\
**Created:** [December 19, 2009, 12:39pm UTC](https://www.ruby-forum.com/t/ruby-1-9-us-ascii-vs-utf-8/180391 "2009-12-19T12:39:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![valafar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d07c76/32.png) [@valafar](https://www.ruby-forum.com/u/valafar)\
**Post date:** [December 19, 2009, 12:39pm UTC](https://www.ruby-forum.com/t/ruby-1-9-us-ascii-vs-utf-8/180391/1 "2009-12-19T12:39:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![valafar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d07c76/32.png) [@valafar](https://www.ruby-forum.com/u/valafar)\
**Post date:** [December 19, 2009, 4:40pm UTC](https://www.ruby-forum.com/t/ruby-1-9-us-ascii-vs-utf-8/180391/2 "2009-12-19T16:40:04Z")

</div>

On Sat, Dec 19, 2009 at 6:39 AM, Petri K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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

---

<div class="post-metadata">

**Author:** ![valafar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d07c76/32.png) [@valafar](https://www.ruby-forum.com/u/valafar)\
**Post date:** [December 19, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/ruby-1-9-us-ascii-vs-utf-8/180391/3 "2009-12-19T16:42:24Z")

</div>

On Sat, Dec 19, 2009 at 10:38 AM, Gregory B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 🙂
