How can I input multibyte characters in rails console (or irb)?
Asked Answered
K

3

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!

Kaylakayle answered 4/1, 2011 at 4:47 Comment(4)
What version of ruby are you using?Cramoisy
@aaron: I am using ruby-1.8.7-p302 and rails 3.0.0Kaylakayle
Which OS/Terminal? That may be part of it.Ism
@jleedev: I am using Mac OS X snowleopard,and the default terminalKaylakayle
K
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.

Kaylakayle answered 5/1, 2011 at 9:9 Comment(3)
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?Manxman
@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?Kaylakayle
FYI without the readline build options irb in 1.9.2 is still brokenPelagianism
B
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
Bauble answered 21/4, 2011 at 10:25 Comment(0)
K
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.

Kaylakayle answered 5/1, 2011 at 9:9 Comment(3)
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?Manxman
@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?Kaylakayle
FYI without the readline build options irb in 1.9.2 is still brokenPelagianism
C
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

Cramoisy answered 4/1, 2011 at 6:10 Comment(1)
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.Kaylakayle

© 2022 - 2024 — McMap. All rights reserved.