From: Tanaka Akira Date: 2010-05-10T12:42:13+09:00 Subject: Re: wcswidth, ruby 1.9, and string encodings 2010/5/10 William Morgan : > > I need some advice about how to use wcswidth on Ruby 1.9 strings. > Wcswidth returns the column width of a string, i.e. how much of the > horizontal space on the screen a string takes up when printed to a > terminal. Being able to calculate column width is crucial for any > console-based program that deals with non-ASCII characters. For example, > Chinese characters typically take up two columns. I wrote such code in my terminfo binding. http://github.com/akr/ruby-terminfo > Unfortunately, this code requires that the string's encoding match your > LC_CTYPE, because mbstowcs relies on LC_CTYPE to produce wchar_t's. If > those don't match, you'll either get nothing, or, possibly, a wrong > answer. I used rb_locale_encoding() to convert to LC_CTYPE as follows: str = rb_str_encode(str, rb_enc_from_encoding(rb_locale_encoding()), 0, Qnil); -- Tanaka Akira