I’m using Ruby 1.9.3 on Windows 7 with Tk interface. In the following simple example, if i click a button, GUI will return me a “??????” string instead of “привет” displayed. Is it possible to get back actual unicode string entered?
#!/usr/bin/env ruby
# coding:utf-8 vi:et:ts=2
require 'tk'
TkRoot.new.tap { |o|
$edit = TkEntry.new( o ).tap { |o|
o.pack( :side => 'left' )
o.insert( 0, "привет" )
}
TkButton.new( o, :text => "click me" ).tap { |o|
o.pack( :side => 'left' )
o.bind( '1' ) {
## In this place i want unicode, but got garbage :(
puts( $edit.get().encoding.name )
puts( $edit.get().inspect )
}
}
o.mainloop()
}

So I checked in Windows and got it working. I’d recommend you put this at the top of your file:
The
# codingbit is unnecessary; the-Kuflag tells Ruby to use the Unicode codepage. While my testing on Mac doesn’t seem to have this issue (either appending-Kuor using# coding: utf-8will work), it is indeed occurring in Windows. I’m running the same versions as you, just in Parallels.Alternatively, you could delete the shebang part and run the file with
ruby -Ku test.rbOriginal answer:
Yes, although the solution I’m accustomed to would be using UTF-8. You just have to put
# coding: utf-8on the top line of your file and Ruby will mystically switch over to processing strings in UTF-8:For further reading, I’d suggest this link which goes over how Ruby thinks about encoding.