Reading UTF-8 lines and writing to TK text area

Hi:

I’m pretty new to Ruby and was wondering if it is possible to read lines
from a UTF-8 encoded file (some Japanese text in this case) and write
them to a text area in a TK gui. I was able to do this in Python with
unicode strings (e.g., u’\u-someCharCode’) but with my Ruby script the
text is garbled. I’ve seen references to UTF-8 in some Ruby
documentation, but I’m not sure how to proceed. It would be a start if
I could just get Japanese written to my console (I run the Japanese
WinXP so it shouldn’t be a problem).

Thanks,

Matthew

Hi,

···

From: Matthew Huggett mhuggett@zam.att.ne.jp
Subject: reading UTF-8 lines and writing to TK text area
Date: Fri, 13 Feb 2004 19:52:31 +0900
Message-ID: 402CAC30.1030804@zam.att.ne.jp

I was able to do this in Python with
unicode strings (e.g., u’\u-someCharCode’) but with my Ruby script the
text is garbled.

A sample script ‘unicodeout.rb’ may help you.
You’ll be able to find it at ext/tk/sample/demos-en/ on the Ruby
source archive.

For exapmle,

require ‘tk’
t = TkText.new.pack
txt = Tk::UTF8_String('\u65E5\u672C\u8A9E\u306E\u3072\u3089\u304C\u306A, ’ +
‘\u6F22\u5B57\u3068\u30AB\u30BF\u30AB\u30CA’)
t.insert(‘end’, txt)
Tk.mainloop


Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hi,

···

From: Matthew Huggett mhuggett@zam.att.ne.jp
Subject: reading UTF-8 lines and writing to TK text area
Date: Fri, 13 Feb 2004 19:52:31 +0900
Message-ID: 402CAC30.1030804@zam.att.ne.jp

I’m pretty new to Ruby and was wondering if it is possible to read lines
from a UTF-8 encoded file (some Japanese text in this case) and write
them to a text area in a TK gui.

Did you try “Tk.encoding = ‘utf-8’”?
For example,

require ‘tk’
Tk.encoding = ‘utf-8’
txt = TkText.new.pack
IO.foreach(‘a_UTF-8_encoded_file’) {|line| txt.insert(‘end’, line)}
Tk.mainloop


Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)