# Newbie qusetion, about unicode

**URL:** <https://www.ruby-forum.com/t/newbie-qusetion-about-unicode/123034>\
**Category:** Rails\
**Created:** [December 13, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/newbie-qusetion-about-unicode/123034 "2007-12-13T15:39:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xpol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/5fc32e/32.png) [@xpol](https://www.ruby-forum.com/u/xpol)\
**Post date:** [December 13, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/newbie-qusetion-about-unicode/123034/1 "2007-12-13T15:39:46Z")

</div>

Hello,  
I’m a beginner to Rails.  
I has a problem about unicode when I tried some example code like  
below:

# say\_controller.rb:

# class SayController \< ApplicationController def list @files = Dir.glob(’\*’) end end

# application.rb:

class ApplicationController \< ActionController::Base

# Pick a unique cookie name to distinguish our session data from

others’  
session :session\_key =\> ‘\_HelloRails\_session\_id’  
before\_filter :configure\_charsets

def configure\_charsets  
response.headers[“Content-Type”] = “text/html; charset=utf-8”

# suppress(ActiveRecord::StatementInvalid) do

# ActiveRecord::Base.connection.execute ‘SET NAMES utf8’

# end

# end end

# list.rhtml:
 File List 
 \<% for file in @files %\> - \<%= file %\>
 \<% end %\> 
 =========

When there is a directory with Chinese characters, I got wrong  
characters (the are in GB18030), why it was not in UTF-8?

---

<div class="post-metadata">

**Author:** ![xpol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/5fc32e/32.png) [@xpol](https://www.ruby-forum.com/u/xpol)\
**Post date:** [December 13, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/newbie-qusetion-about-unicode/123034/2 "2007-12-13T17:40:59Z")

</div>

On Dec 13, 2007, at 6:39 AM, xpol wrote:

> I has a problem about unicode when I tried some example code like  
> below:  
>   
> When there is a directory with Chinese characters, I got wrong  
> characters (the are in GB18030), why it was not in UTF-8?

I haven’t sorted out all UTF-8 yet, but I’ve learned that you need to  
add this to the top of the environment.rb file:

$KCODE = ‘UTF8’

Also, be sure to use Rails’ Multibyte API when working with UTF8  
strings.

> **[ActiveSupport::Multibyte::Chars](https://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html)**
>
> Chars enables you to work transparently with UTF-8 encoding in the Ruby String class without having extensive knowledge about the encoding.

I don’t know if these will solve all your needs, but it’s the place  
to start.

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end

---

<div class="post-metadata">

**Author:** ![xpol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/5fc32e/32.png) [@xpol](https://www.ruby-forum.com/u/xpol)\
**Post date:** [December 14, 2007, 2:28am UTC](https://www.ruby-forum.com/t/newbie-qusetion-about-unicode/123034/3 "2007-12-14T02:28:22Z")

</div>

Thanks Greg,

```
I thikn the problem is, the

```

@files = Dir.glob(’\*’)

> 

got strings that is not in UTF-8 encoding, and the I tried to trade them  
as  
UTF-8. There may needs conversions.

BTW, is

$KCODE = ‘UTF8’

the same as

$KCODE = ‘u’

> 

–  
Best Regards!

Xpol Wan
