7

guys. I am developing a Chinese application with rails. Now I want to input some Chinese characters in rails console but cannot do that, either in irb.

Any guys who have the experience to solve this problem? I would appreciate your help!

Jimmy Huang
  • 4,252
  • 2
  • 22
  • 22

3 Answers3

5

Based on @Jimmy-Huang's answer, these are the steps I followed on Mac Leopard using rvm and ruby 1.9.2:

rvm package install readline
rvm remove 1.9.2
rvm install 1.9.2 --with-readline-dir=$rvm_path/usr

That resulted in some errors, particularly when trying bundle install:

uninitialized constant Gem::SilentUI

It turned out that's due to an older version of bundler and this gets rid of it:

gem install bundler
Sjors Provoost
  • 1,921
  • 1
  • 19
  • 34
2

I found the solution for me, it need to re-compile the readline. And now I can input non-ASCII characters!

Because I am using rvm, so I found this article to teach you how to re-compile readline under rvm. http://rvm.beginrescueend.com/packages/readline/

And for someone who is not using rvm, maybe you can follow this post and have a try: http://henrik.nyh.se/2008/03/irb-readline

By the way, ruby-1.9.2 irb already supports non-ASCII inputing.

Jimmy Huang
  • 4,252
  • 2
  • 22
  • 22
  • I've been wondering about this as well. I didn't realize 1.9.2 supported it natively, so I tried it and still couldn't input non-ASCII characters. Then I realized it's because I have Terminal set to use the option key as a meta key. I turned it off and then I could type non-ASCII characters, but it broke the left and right arrow keys. Have you found a solution to use both non-ASCII characters and the arrow keys? – Jimmy Jan 05 '11 at 09:17
  • @Cuadra: I am not turning on the 'use the option key as a meta key', but still can input non-ASCII characters. I don't know why the meta key will stop you from inputting. Can you explain it more? – Jimmy Huang Jan 05 '11 at 09:58
  • FYI without the readline build options irb in 1.9.2 is still broken – Julik Aug 24 '11 at 13:15
1

Check out the pack method on array: http://ruby-doc.org/core-1.8.7/classes/Array.html#M000287

I think you'd want:

[111 ,222, 333].pack(U*)

Here is an interesting discussion on the subject had with Matz:

http://www.ruby-forum.com/topic/134919

Aaron Scruggs
  • 712
  • 1
  • 4
  • 10
  • Thank you @aaron. You provide a way to input Chinese character, I can use pack method to construct some random Chinese words now. But if I want to input a particular word, I have to know the ASCII code of each character. If I can input characters directly in console, that will be more wonderful. – Jimmy Huang Jan 04 '11 at 06:52