问题描述
我正在Ubuntu 10.04 64位上运行带有Rails 2.3.5的Ruby 1.8.7.我编写了一种方法,该方法应采用这样的字符串"École À la Découverte"
并输出诸如"ecole_a_la_decouverte"
这样的文件系统名称:
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('_')
当我在代码中测试此行时,测试总是失败,指出"cole_la_dcouverte"
不等于"ecole_a_la_decouverte"
.奇怪的是,如果我插入调试器行并使用调试器控制台,则测试通过.同样,在irb
和./script/console
中手动运行此行似乎可行.
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.
有人知道发生了什么,为什么这个测试失败了?我唯一的想法是,包括调试器或irb
会以某种方式增加对UTF-8的支持,但我不知所措,想知道下一步该怎么做.
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.
提前谢谢!
推荐答案
这似乎是语言环境的问题-在php中也观察到了同样的问题,如果有的话,其中的iconv函数有时不起作用没有语言环境设置(无论任何语言环境).
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).
请参见 ruby-forum.com上的主题,其中建议使用级别修复程序,该级别修复程序允许您使用C中的setlocale()
设置语言环境.
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.
另一建议是安装语言环境gem .
这篇关于Ruby Iconv可与irb和ruby调试器一起使用,但不能在单元测试中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!