Issue #4491 has been updated by Joey Zhou.


Is this issue forgotten? :)
----------------------------------------
Bug #4491: Some methods of Dir/File do not encode dirname/filename transparently
http://redmine.ruby-lang.org/issues/4491

Author: Joey Zhou
Status: Open
Priority: Normal
Assignee: 
Category: 
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