ruby

Is it possible to use Ruby temp directory engine with non-english file names on Windows?


I am creating a temporary directory in Ruby using 'tmpdir', and adding a file in the temporary directory that has a non-English file name:

#!/usr/bin/env ruby -KU
# coding:utf-8

require 'tmpdir'
Dir.mktmpdir { |dir| File.open( "#{dir}/файл.txt", "w" ) {} }

The program fails on cleanup, attempting to delete "????.txt". I can see the file is being created with the appropriate name.

I am running Ruby 1.9 on Windows. Is there some way to fix this, or is Ruby 1.9 not intended to be used with non-English characters on Windows?


Solution

  • This is a bug introduced more than 2 years ago by typo. Will be fixed in Ruby 2.0 :( https://github.com/ruby/ruby/commit/5cfba33a77d73bc09df850858ccd8ed8eb7014d4

    For ruby version < 2.0 this hack defined after require 'tmpdir' will fix a problem:

    if RUBY_VERSION < '2.0' then
      ##  Fix bug
      class FileUtils::Entry_
        def entries
          opts = {}
          opts[:encoding] = "UTF-8" if /mswin|mingw/ =~ RUBY_PLATFORM
          Dir.entries(path(), opts)\
              .reject {|n| n == '.' or n == '..' }\
              .map {|n| FileUtils::Entry_.new(prefix(), join(rel(), n.untaint)) }
        end
      end
    end