# $KCODE and encodings

**URL:** <https://www.ruby-forum.com/t/kcode-and-encodings/160305>\
**Category:** IronRuby\
**Created:** [February 14, 2009, 1:48am UTC](https://www.ruby-forum.com/t/kcode-and-encodings/160305 "2009-02-14T01:48:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shri\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Shri\_B](https://www.ruby-forum.com/u/Shri_B)\
**Post date:** [February 14, 2009, 1:48am UTC](https://www.ruby-forum.com/t/kcode-and-encodings/160305/1 "2009-02-14T01:48:56Z")

</div>

I was searching for string encoding issues in Ruby. Here is the summary  
of what I learnt, in case its useful to anyone else of if anyone has any  
corrections to this.

Ruby 1.8 support for encoding:

- 

```
    A comment like "# -*- coding: utf-8 -*-" at the start of the 

```

file is supposed to determine how to parse a .rb file, but I haven’t  
really figured out how to make this work. Non-ansi characters cause an  
error while loading the file.

- 

```
    ruby.exe -K<kcode> sets $KCODE (which can also be set 

```

programmaticaly)

- 

```
    $KCODE affects the following:

```

- 

```
    Determines the encoding to use to parse .rb files. Normally, 

```

identifiers have to be ANSI, but the limitation is removed if $KCODE is  
set to “UTF8”.

- 

```
    Affects whether inspect escapes non-ascii chars, or if it 

```

leaves them as is.

- 

```
    Affects how regexps without an explicit encoding interpret the 

```

input string.

Ruby 1.9 support for encodings:

- 

```
    Identifiers can be non-ANSI by default.

```

Ruby 2.0 support for encodings:

- 

```
    Each string and symbol knows its own encoding, and 

```

String#force\_encoding can change the encoding of an existing string.

- 

```
    IO#encoding to control encoding to use for reading/writing 

```

from disk

---

<div class="post-metadata">

**Author:** ![Shri\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Shri\_B](https://www.ruby-forum.com/u/Shri_B)\
**Post date:** [February 14, 2009, 2:11am UTC](https://www.ruby-forum.com/t/kcode-and-encodings/160305/2 "2009-02-14T02:11:09Z")

</div>

On Fri, Feb 13, 2009 at 5:01 PM, Shri B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Ruby 1.8 support for encoding:
> 
> Â· A comment like “# -_- coding: utf-8 -_-” at the start of the  
> file is supposed to determine how to parse a .rb file, but I haven’t really  
> figured out how to make this work. Non-ansi characters cause an error while  
> loading the file.

Did the utf-8 file(s) you tried have a BOM or not?

-Matthew

---

<div class="post-metadata">

**Author:** ![Shri\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Shri\_B](https://www.ruby-forum.com/u/Shri_B)\
**Post date:** [February 14, 2009, 3:09am UTC](https://www.ruby-forum.com/t/kcode-and-encodings/160305/3 "2009-02-14T03:09:10Z")

</div>

If I use Notepad2’s menu to set the encoding to “UTF8 with signature”,  
and run either “ruby utf8\_with\_signature.rb” or “ruby -Ku  
utf8\_with\_signature.rb”, the file fails to parse. The file is attached.

If I save the file with encoding set just as “UTF8”, the file is 3 bytes  
smaller. “ruby utf8.rb” fails, but “ruby -Ku utf8.rb” works. With “-Ku”,  
things work even if I do not have “# -_- coding: utf-8 -_-” in the file.

The repro files are attached.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Matthew Wilson  
Sent: Friday, February 13, 2009 5:11 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] $KCODE and encodings

On Fri, Feb 13, 2009 at 5:01 PM, Shri B.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Ruby 1.8 support for encoding:

- 

```
    A comment like "# -*- coding: utf-8 -*-" at the start of the 

```

file is supposed to determine how to parse a .rb file, but I haven’t  
really figured out how to make this work. Non-ansi characters cause an  
error while loading the file.

Did the utf-8 file(s) you tried have a BOM or not?

-Matthew

---

<div class="post-metadata">

**Author:** ![Shri\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Shri\_B](https://www.ruby-forum.com/u/Shri_B)\
**Post date:** [February 14, 2009, 3:48am UTC](https://www.ruby-forum.com/t/kcode-and-encodings/160305/4 "2009-02-14T03:48:58Z")

</div>

AFAIK Ruby 1.8 doesn’t support magic comments that specify encodings at  
all, 1.9 does. Ruby 1.8 also doesn’t recognize BOM.  
Even version 1.9 has full encoding support, not just 2.0.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Shri B.  
Sent: Friday, February 13, 2009 3:01 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] $KCODE and encodings

I was searching for string encoding issues in Ruby. Here is the summary  
of what I learnt, in case its useful to anyone else of if anyone has any  
corrections to this.

Ruby 1.8 support for encoding:

- 

```
    A comment like "# -*- coding: utf-8 -*-" at the start of the 

```

file is supposed to determine how to parse a .rb file, but I haven’t  
really figured out how to make this work. Non-ansi characters cause an  
error while loading the file.

- 

```
    ruby.exe -K<kcode> sets $KCODE (which can also be set 

```

programmaticaly)

- 

```
    $KCODE affects the following:

```

- 

```
    Determines the encoding to use to parse .rb files. Normally, 

```

identifiers have to be ANSI, but the limitation is removed if $KCODE is  
set to “UTF8”.

- 

```
    Affects whether inspect escapes non-ascii chars, or if it 

```

leaves them as is.

- 

```
    Affects how regexps without an explicit encoding interpret the 

```

input string.

Ruby 1.9 support for encodings:

- 

```
    Identifiers can be non-ANSI by default.

```

Ruby 2.0 support for encodings:

- 

```
    Each string and symbol knows its own encoding, and 

```

String#force\_encoding can change the encoding of an existing string.

- 

```
    IO#encoding to control encoding to use for reading/writing 

```

from disk
