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

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!

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

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.

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

Related

Ruby Gems suddenly stopped working on ubuntu 12.04

After a couple of days of not doing pretty much anything on the ubuntu box, I decided to try out some ruby stuff. For this, I wanted to fire up pry. Unfortunately, I was presented with
Sorry, you can't use Pry without Readline or a compatible library.
Please gem install rb-readline or recompile Ruby --with-readline.
~/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
`require': libreadline.so.5: cannot open shared object file: No such
file or directory -
~/.rbenv/versions/2.1.1/lib/ruby/2.1.0/x86_64-linux/readline.so
(LoadError)
Being quite new to linux in general, I figured I'd just do as it said, and install this rb-readline. The installation passes just fine, but doesnt seem to do anything at all. If I do gem list p, the package just doesn't appear.
So yeah, I just don't know what to do to get it working again, and the only thing I managed to find by searching was related to an installation without rbenv and had something to do with sudo etc.
Any ideas?
Without understanding the problem and following the advice in option 2 of the message:
Sorry, you can't use Pry without Readline or a compatible library.
Possible solutions:
* Rebuild Ruby with Readline support using `--with-readline`
* Use the rb-readline gem, which is a pure-Ruby port of Readline <==== Option 2
* Use the pry-coolline gem, a pure-ruby alternative to Readline
I added the gem into the Gemfile (as follows), bundled and pry was then available.
group :development, :test do
gem 'pry'
gem 'rb-readline'
end
I had this problem too. I am using rbenv and reinstalling ruby via
rbenv install -f 2.2.3
did fixed it for me. Of course you'd put in your respective version. -f forces the installation even though you already have that specific version installed. use rbenv global to find out what version you have installed and set.
I have some problem. But I don't want to add gem 'rb-readline'. So try it...
$ln -s /usr/local/opt/readline/lib/libreadline.8.0.dylib /usr/local/opt/readline/lib/libreadline.7.dylib
Do a sudo apt-get install libreadline-dev, seems like you're missing the readline shared library that pry is wanting. If it worked before, not sure why the library isn't there anymore.
I ended up doing rbenv uninstall, followed by rbenv install and re-installing all the gems, and got it back to working. Still no idea what caused it in the first place, but it works now.

How to install/use Ruby Shoes (without Hackety Hack)?

SOLUTION:
You have the same problem, you have to compile Shoes (https://github.com/shoes/shoes/wiki/Building-Shoes-on-Linux)
if rake doesn't work, try to install ruby 1.9.2 through rvm.
I recently started to learn Ruby, and i came across this library.
I tried to install it using the .run file provided by the official site, but it seems like it's broken. If i run it, it tells me:
./shoes-3.1.0.run: 1: eval: ./shoes: not found
And yes, after extracting the file, i found a folder which didnt contain it.
The only runnable thing i find is HacketyHack, which worked, but I prefer to use my own editor.
Since I have just started to learn Ruby, I'm not comfortable with playing with the .rb files to get it working outside of HH. I also tried to install it via apt-get, but after running 'shoes', all i get is:
no such file to load -- rubygems
Compiling from source code didn't work for me. Is there anything else I can try, or am I doing something terribly wrong?
The problem seems to be your ruby version. ruby in the apt repositories is version 1.8.7 which is quite old. Try apt-get install ruby1.9.1
If that doesn't work, I would recommend using rvm. Install, then simply rvm install ruby-2.0.0 which is the latest version, then rvm use 2.0.0. If shoes doesn't work under any of these versions, it's probably a bug in shoes.

Can't enter Umlauts in Ruby 1.9.3 IRB

I am experiencing very strange behavior in Ruby 1.9.3's IRB with Mac OS 10.7.3
When I try to enter an Umlaut, it's escaped in the prompt and looks like this (I entered "ΓΌ" on the keyboard)
irb(main):001:0> "\U+FFC3\U+FFBC"
What's super strange is this:
irb(main):001:0> "\U+FFC3\U+FFBC".length
=> 0
Of course, the character isn't displayed either:
irb(main):001:0> "\U+FFC3\U+FFBC"
=> ""
Does anyone know what's going on here or how to fix this?
Victor Moroz didn't quite give the definitive answer but his link led me to a solution (thx!):
I forgot to mention:
Im running homebrew
I built ruby using ruby-build and this recipe (1.9.3-p125-perf, with falcon patches)
What I then did to solve this problem in my case was to recompile, this time pointing ruby to a more recent version of readline (6.2.2 in my case) that I installed with homebrew.
The steps it took were:
$ brew install readline
$ export CPPFLAGS=-I/usr/local/Cellar/readline/6.2.2/include
$ export LDFLAGS=-L/usr/local/Cellar/readline/6.2.2/lib/
$ curl https://raw.github.com/gist/1688857/rbenv.sh | sh && rbenv global 1.9.3-p125-perf
I ran across this problem as well, but my Ruby was built with rvm rather than ruby-build. I'd also previously used Homebrew on Mountain Lion to run other package installers, and among them got Readline 6.2.5 or something like that. This turns out to be the cause of this issue for me. So here's the fix:
From the RVM site:
If you have an error when compiling pertaining to readline, you may need to attempt installing with the procedure defined below.
$ rvm pkg install readline
$ rvm reinstall 1.9.2 --with-readline-dir=$rvm_path/usr
Note that this error occurs most often when having a MacPorts, Fink, or Homebrew installed readline on your system.
Hope this helps someone else!
This may help http://henrik.nyh.se/2008/03/irb-readline
UPDATE In fact on my Mac I have no problem with port version of Ruby, but original Mac OS version does have this problem (never used that one). So the easiest way is probably just to install Ruby from ports.
this command solved it for me:
brew install readline
RUBY_CONFIGURE_OPTS=--with-readline-dir="$(brew --prefix readline)" rbenv install 1.9.3-p286
source: https://github.com/rbenv/ruby-build/wiki

Problem with ri + rvm + Textmate

I'm trying to lookup help for Ruby methods in Textmate through the Ctrl+H shortcut, but I'm always getting this error:
/Users/tomdeleu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:388:in
bin_path': can't find gem rdoc ([">=
0"]) with executable ri
(Gem::GemNotFoundException) from
/Users/tomdeleu/.rvm/gems/ruby-1.9.2-p180/bin/ri:19:in
'
Platform:
MacOS X 10.6.8
RVM 1.6.20
Textmate 1.5.10 (1631)
TM_RUBY set to /Users/tomdeleu/.rvm/bin/rvm-auto-ruby
installed osx-plist update as detailed in the RVM integration docs
installed DrNic's Rails bundle
Ri works fine from the command-line (I ran "rvm docs generate" to install docs for 1.9.2).
I've searched around but can't find a solution. Can anyone help?
After a lot of searching (and asking) around, solved it by reinstalling both Textmate (including delete of all customizations), and Ruby 1.9.2 (through RVM). Afterwards I installed the osx-plist update + some bundles again. Note that the osx-plist update (as detailed here) should be done with system Ruby, not a Ruby 1.9, otherwise other errors occur. Also in Textmate make sure the PATH variable does not have a custom Ruby before your system Ruby.
With these settings, everything works fine now. Hopefully this is useful to anyone encountering this error in the future...

fubar'd a ruby installation on ubuntu, need helping purging it for reinstall

i originally followed these steps to get a RoR environmentgoing: https://help.ubuntu.com/community/RubyOnRails
that worked. then i put 1.9.2 on here. then i decided i want the original setup, did a "whereis ruby" and deleted those directories manually.
that fubar'd my ruby installation. i can't use the repository to what i originally had. how can i purge ruby from completely and start fresh?
right now the specific error is this:
/usr/lib/ruby/1.8/rubygems.rb:11:in `require': no such file to load -- thread (LoadError)
from /usr/lib/ruby/1.8/rubygems.rb:11
from /usr/bin/gem1.8:8:in `require'
from /usr/bin/gem1.8:8
tipu#tipu_ubuntu:~$
You're not giving enough real information to be specific, but this is what I'd try:
Use apt-search to locate Ruby 1.8.7 for your OS version.
Using apt-get, uninstall that version so you're back to what APT thinks is a clean slate.
Again using apt-get, reinstall that version so you're back to what APT thinks is a normal installation.
From that point, I would STOP messing with the system Ruby, and instead use RVM to manage and install any new Ruby and gems installations into RVM's ~/.rvm sandbox.
To follow up on the Tin Man's answer:
I wrote a guide to installing Ruby and Rails on Ubuntu which you may find useful for getting this all peachy again.
Hope it helps!
You need to reinstall libruby1.8. I'm pretty sure that will take care of it. I had exactly the same problem.

Resources