From: Yui NARUSE Date: 2011-04-26T18:16:06+09:00 Subject: [ruby-core:35902] [Ruby 1.9 - Bug #4491][Assigned] Some methods of Dir/File do not encode dirname/filename transparently Issue #4491 has been updated by Yui NARUSE. Category set to M17N Status changed from Open to Assigned Assignee set to Yui NARUSE ---------------------------------------- Bug #4491: Some methods of Dir/File do not encode dirname/filename transparently http://redmine.ruby-lang.org/issues/4491 Author: Joey Zhou Status: Assigned Priority: Normal Assignee: Yui NARUSE Category: M17N Target version: ruby -v: ruby 1.9.2p180 (2011-02-18) [i386-mingw32] My Ruby version is: ruby 1.9.2p180 (2011-02-18) [i386-mingw32] My OS is Windows 7 (Simplified Chinese), its cmd.exe has a default codepage of CP936 (aka GBK), but I want my scripts to be written in UTF-8, so I've set the scripts' to UTF-8 and add "# encoding: utf-8" on the first line. I find when I open a file, the filename will be encoded to the system's locale charmap automatically: puts File.open( "������file" ).readline #this will be fine, although the "������file" literal is UTF-8 puts File.open( "������file".encode('utf-8') ).readline #explicitly encoded puts File.open( "������file".encode('gbk') ).readline puts File.open( "������file".encode('big5') ).readline puts File.open( "������file".encode('shift_jis') ).readline #all fine, all encoded to Encoding.locale_charmap before asking the OS for the file, no matter to what it's encoded here I have tested many methods of File & Dir class, most of them have this intimate and convenient feature, but some are not: p Dir.entries( "������dir".encode('gbk') ) # explicit encode is required, or "No such file or directory" error. Since my local charmap is gbk, it can not be encoded to utf-8, big5, shift_jis p Dir.glob( "������dir/*".encode('gbk') ) Dir.foreach( "������dir".encode('gbk') ) {|f| puts f} Dir.open( "������dir".encode('gbk') ).each {|f| puts f} Dir.new( "������dir".encode('gbk') ).each {|f| puts f} # all must explicitly encoded to locale_charmap (my situation is GBK) and a File method require encoding too: puts File.absolute_path( "������file".encode('gbk') ) These methods may be missed, they may should act the same way as Dir.chdir("������dir") or File.open("������dir"), with a feature of transparently encoding. Best regards. Joey -- http://redmine.ruby-lang.org