问题
I'm running Ruby 1.8.7 with Rails 2.3.5 on Ubuntu 10.04 64-bit. I've written a method that should take a string like this, "École À la Découverte"
and output a file-system name like this "ecole_a_la_decouverte"
:
(Iconv.new('US-ASCII//TRANSLIT', 'utf-8').iconv "École À la Découverte").downcase.split.join('_')
When I test this line in my code, the test always fails saying that "cole_la_dcouverte"
is unequal to "ecole_a_la_decouverte"
. The odd thing is that if I insert a debugger line and use the debugger console the test passes. As well, running this line manually in irb
and ./script/console
seems to work.
Does anyone know what's going on and why this test is failing? My only thought is that including the debugger or irb
somehow adds more support for UTF-8 but I'm at a loss to figure out where to go next.
Thanks in advance!
回答1:
This appears to be a problem with the locale - the same which is also observed in php, where (sometimes?) iconv functions don't work if there is no locale set (whatever the locale).
See a thread on this on ruby-forum.com where a low-level fix is suggested which allows you to set a locale using setlocale()
in C.
Another suggestion is to install the locale gem.
来源:https://stackoverflow.com/questions/3046462/ruby-iconv-works-with-irb-and-ruby-debugger-but-not-in-a-unit-test