Hello all,
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.
regedit
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\RubyFile\shell\open\command =
"C:\Ruby193\bin\ruby.exe" "%1" %*
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.
How can I solve this problem ? I would like to keep the double click
functionality, without having to resort to batch file wrappers.
Thanks for your input.
on 2012-10-01 11:06
on 2012-10-01 15:14
S. Hieko wrote in post #1078130: > Hello all, > > 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. > > regedit > HKEY_LOCAL_MACHINE\SOFTWARE\Classes\RubyFile\shell\open\command = > "C:\Ruby193\bin\ruby.exe" "%1" %* > > 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. Jon
on 2012-10-01 16:11
Jon Forums wrote in post #1078172: > > 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.
on 2012-10-01 18:31
Just for be sure, did you tried stablish the default encoding of the source code to utf-8, and in the options of your text editor? #encoding: utf-8 at the start of the file.
on 2012-10-01 18:32
> 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?
on 2012-10-01 19:27
>> 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
on 2012-10-02 01:05
Thanks Jon and Damián.
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:
1. Add the "#encoding: utf-8" magic tag at the top of the script. This
forces string output to UTF8 whatever happens.
2. 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.
3. 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.
Thanks again for your help!
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.