Ruby Iconv works with irb and ruby ​​debugger, but not in unit test

I am running Ruby 1.8.7 with Rails 2.3.5 on Ubuntu 10.04 64-bit. I wrote a method that should take a line like this, "École À la Découverte" and print 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 check this line in my code, the test always fails saying that "cole_la_dcouverte" does not match "ecole_a_la_decouverte" . It is strange that if I insert a debugger line and use the debugger console, the test passes. Also, this line works manually in irb and ./script/console seems to work.

Does anyone know what is happening and why this test fails? My only thought is that turning on the debugger or irb somehow adds extra support for UTF-8, but I don’t understand where to go next.

Thanks in advance!

+4
source share
1 answer

This is apparently a locale problem - the same thing that is also observed in php, where (sometimes?) The iconv functions do not work if there is no language set (regardless of the locale).

See the thread on this at ruby-forum.com for a low-level fix that allows you to set the locale using setlocale() in C.

Another suggestion is to set a yellow pattern .

0
source

Source: https://habr.com/ru/post/1312885/


All Articles