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

一世执手 提交于 2019-11-30 03:58:48

问题


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!


回答1:


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



回答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.




回答3:


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



来源:https://stackoverflow.com/questions/4590725/how-can-i-input-multibyte-characters-in-rails-console-or-irb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!