Encoding of parameter strings substituted into TkEntry validatecommand arguments

The program below uses a TkEntry widget with a 'validatecommand',
where the command takes two arguments that are supplied by
Tk by substituting the old and new value respectively of the
TkEntry widget, for the strings '%s' and '%P'.

The Ruby implementation generates a piece of Tcl code that calls the
Ruby validation proc, and passes the piece to the Tcl interpreter.

However, when this piece of Tcl code is invoked and the Ruby side
is called, the strings substituted are not correctly tagged with their
encoding.

Since version 8.1, tcl and tk uses UTF-8 internally, and the strings
that tcl substitute for the %s and %P codes, are in UTF-8.
This is true independent of the Tk.encoding in force.

Yet, on the Ruby side, these strings are tagged as ASCII-8BIT,
which really means "Mixed data, bytes below 128 are ASCII,
other bytes are binary data of unknown nature".

If the user types non-ascii characters into the TkEntry widget,
Ruby does not know how to convert the data into any encoding.
The solution is to call use arg.force_encoding('UTF8').

----8<---- Code begins ------8<---------
class String
     def fix_encoding
         if encoding.name == 'ASCII-8BIT'
             s = force_encoding('UTF-8')
         else
             s = encode('UTF-8')
         end
         return s
     end
end

require 'tk'
Tk.encoding='UTF-8'
root = TkRoot.new { title "Tk encoding checker" }

l = TkLabel.new(root) { text "Type your string here" }

e = TkEntry.new(root) do
     validate :key
     validatecommand [
         proc {|o,n|
             puts "Validating: old: #{o.encoding.name.fix_encoding}: '#{o.fix_encoding}'"
             puts " new: #{n.encoding.name.fix_encoding}: '#{n.fix_encoding}'"
             puts "(By the way, the encoding name is in #{o.encoding.name.encoding.name.fix_encoding})"
             STDOUT.flush
             true
         },
         '%s', '%P'
     ]
end

b = TkButton.new(root) do
     text "Show"
     command proc {
         puts "Getting the value: #{e.get.encoding.name.fix_encoding}: #{e.get.fix_encoding}"
         STDOUT.flush
     }
end

b.pack(:padx=>15, :pady=>15, :side=>:bottom)
l.pack(:padx=>15, :pady=>15, :side=>:left)
e.pack(:padx=>15, :pady=>15, :side=>:left)

Tk.mainloop
----8<---- Code ends ------8<---------

Typical output from the program, typing 'Æ' into the TkEntry and then clicking the button:

----8<---- Code begins ------8<---------
$ ruby test-encoding.rb
Validating: old: ASCII-8BIT: ''
             new: ASCII-8BIT: 'Æ'
(By the way, the encoding name is in US-ASCII)
Getting the value: UTF-8: Æ
----8<---- Code ends ------8<---------

I am on a Windows 10 computer with Ruby 2.2.0 and Tcl/Tk 8.5.
If I omit the statement "Tk.encoding='UTF-8'" at the top,
then the value of the TkEntry widget -- the last line in the output -- is in CP850.

I think that this can be improved for all Tcl/Tk versions above 8.2.
The whole piece of tcl callback code should be converted to UTF-8 before
it is passed to the Tcl interpreter, and all arguments given to the Ruby side
should be tagged UTF-8.

Regards