I’ve installed ruby 1.9.3 on a Windows XP box (french) for work related
purposes, with ruby installer from rubyinstaller.org. (same thing
happened on 1.9.2)
Here is the content of a very simple script #SCRIPT_START
puts “This should work, press a key to exit”
gets #SCRIPT_END
The script executes normally if saved to a path such as
“C:\Scripts\Ok\test.rb”
However if saved to a path with an accent for instance
“C:\Scripts\Cliché\test.rb”, I get an instant failure if I double click
on the script file.
Going to the command line, if I type:
C:\Scripts\Ok\test.rb => execution is ok.
C:\Scripts\Cliché\test.rb => returns following error
“C:/Scripts/ClichÚ/test.rb: No such file or directory -
C:/Scripts/ClichÚ (Errno::ENOENT)”
ruby C:\Scripts\Cliché\test.rb => execution is ok.
This problem also causes failure if I need to use “require” pointing to
other scripts. For instance if I type inside irb:
require “C:\Scripts\Ok\test.rb” => execution is ok
require “C:\Scripts\Cliché\test.rb” => failure to find file
I have noticed there are many encodings being used all over the place.
For instance in irb :
Dir.pwd.encoding.name returns “Windows-1252”
“test”.encoding.name returns “CP850”
So require “C:\Scripts\Cliché\test.rb”.encode(“Windows-1252”) seems
to solve that problem.
I’ve installed ruby 1.9.3 on a Windows XP box (french) for work related
purposes, with ruby installer from rubyinstaller.org. (same thing
happened on 1.9.2)
Here is the content of a very simple script #SCRIPT_START
puts “This should work, press a key to exit”
gets #SCRIPT_END
The script executes normally if saved to a path such as
“C:\Scripts\Ok\test.rb”
However if saved to a path with an accent for instance
“C:\Scripts\Cliché\test.rb”, I get an instant failure if I double click
on the script file.
Going to the command line, if I type:
C:\Scripts\Ok\test.rb => execution is ok.
C:\Scripts\Cliché\test.rb => returns following error
“C:/Scripts/ClichÚ/test.rb: No such file or directory -
C:/Scripts/ClichÚ (Errno::ENOENT)”
ruby C:\Scripts\Cliché\test.rb => execution is ok.
This problem also causes failure if I need to use “require” pointing to
other scripts. For instance if I type inside irb:
require “C:\Scripts\Ok\test.rb” => execution is ok
require “C:\Scripts\Cliché\test.rb” => failure to find file
I have noticed there are many encodings being used all over the place.
For instance in irb :
Dir.pwd.encoding.name returns “Windows-1252”
“test”.encoding.name returns “CP850”
So require “C:\Scripts\Cliché\test.rb”.encode(“Windows-1252”) seems
to solve that problem.
So I guess the ruby interpreter is expecting the script name as a string
in another encoding than that which is given by the windows subsystem.
Looks like your cmd.exe defaults to codepage 850 and ruby doesn’t like
it. In an open Command Prompt set the active codepage to 1252 and run
your command line tests again.
chcp 1252
If the problem disappears, consider changing your default codepage by
setting the OEMCP subkey of
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage to
1252. Once you’ve rebooted, any Command Prompt you open will be in 1252
mode.
Looks like your cmd.exe defaults to codepage 850 and ruby doesn’t like
it. In an open Command Prompt set the active codepage to 1252 and run
your command line tests again.
chcp 1252
If the problem disappears, consider changing your default codepage by
setting the OEMCP subkey of
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage to
1252. Once you’ve rebooted, any Command Prompt you open will be in 1252
mode.
Jon
Thanks. Changing the command prompts code page did indeed allow me to
run the script by specifying the script’s path only.
However the script’s path displays incorrectly inside the command prompt
itself, as I type it (“C:/Scripts/ClichÚ/test.rb”) and any command’s
output such as “dir” appears wrong as well.
I changed the reg key + reboot to see if I could launch the file within
windows explorer but it didn’t have any effect, still not working.
However the script’s path displays incorrectly inside the command prompt
itself, as I type it (“C:/Scripts/ClichÚ/test.rb”) and any command’s
output such as “dir” appears wrong as well.
Perhaps a font issue. Verify that command prompt is using a unicode
capable font like Lucida Console and not Raster Fonts.
I changed the reg key + reboot to see if I could launch the file within
windows explorer but it didn’t have any effect, still not working.
This will likely just open up a command prompt, run the script, and
close the command prompt so you’ll see just a flash of the
prompt appearing/disappearing, right?
However the script’s path displays incorrectly inside the command prompt
itself, as I type it (“C:/Scripts/ClichÚ/test.rb”) and any command’s
output such as “dir” appears wrong as well.
Perhaps a font issue. Verify that command prompt is using a unicode
capable font like Lucida Console and not Raster Fonts.
On Win7 with Consolas as cmd.exe font:
C:\Users\Jon\Downloads\temp>chcp
Active code page: 1252
C:\Users\Jon\Downloads\temp>type Cliché\test.rb
puts “This should work, press a key to exit”
gets
C:\Users\Jon\Downloads\temp>ruby -v Cliché\test.rb
ruby 1.9.3p277 (2012-09-25 revision 37029) [i386-mingw32]
This should work, press a key to exit
I’m sorry I got confused with all the trial and error.
I had removed temporarily the “gets” statement while launching via
windows explorer. The code page regkey change does indeed modify the
explorer’s behaviour (and so it works!). The code page trick was the
right thing to do. The garbage output was solved with the font change as
suggested.
So for anyone googling here is what worked for me:
Add the “#encoding: utf-8” magic tag at the top of the script. This
forces string output to UTF8 whatever happens.
Save the file in UTF-8 encoding. Otherwise it seems the ruby
interpreter will try to interpret the bytes of the string (as they are
inside the text file) as if they were utf8 encoded.
Append to any strings representing paths with accents, etc. the
method “.encode(‘Windows-1252’)”. This seems to be necessary for the
same reason as the code page change was needed. For instance I had a
weird error : although a path variable had been declared at the top of
the script, failure occured later on while I was appending a path value
to an array inside a block. It’s the only way I could use the “require”
statement as well.
1 and 2 are necessary to have consistency (for instance irb and pry
return different strings encoding).
I could bypass 1, 2 and 3 by just specifying the magic tag “#encoding:
Windows-1252” at the start of the script by since I’m parsing external
data I chose not to.