quote stripping in windows ruby.exe command line

T

Timothy Byrd

Given this one-line Ruby script:

puts "ARGV[0] is #{ARGV[0]}"

ruby.exe (1.8.2 on Windows)

[C:\] c:\ruby\bin\ruby.exe argvtest.rb "quoted"
ARGV[0] is quoted

Should ruby.exe be stripping the quotes? How can I pass in a string
with quotes?

-- Timothy
 
W

William James

Timothy said:
Given this one-line Ruby script:

puts "ARGV[0] is #{ARGV[0]}"

ruby.exe (1.8.2 on Windows)

[C:\] c:\ruby\bin\ruby.exe argvtest.rb "quoted"
ARGV[0] is quoted

Should ruby.exe be stripping the quotes? How can I pass in a string
with quotes?

-- Timothy

ruby.exe argvtest.rb '"quoted"'

ruby.exe argvtest.rb "\"quoted\""
 
A

Austin Ziegler

Given this one-line Ruby script:

puts "ARGV[0] is #{ARGV[0]}"

ruby.exe (1.8.2 on Windows)

[C:\] c:\ruby\bin\ruby.exe argvtest.rb "quoted"
ARGV[0] is quoted

Should ruby.exe be stripping the quotes? How can I pass in a string
with quotes?

This is not Ruby doing this.

-austin
 
T

Timothy Byrd

[hoping this isn't a double post]

William said:
ruby.exe argvtest.rb '"quoted"'

ruby.exe argvtest.rb "\"quoted\""

Thanks. I didn't get this at first because my shell uses a caret (^)
as the escape character, not the backslash - backslash being the
directory separator on the PC.

After reading your reply, I realized I needed to go through the code to
figure out how Ruby is really behaving. For example, the MSVC runtime
strips out all quotes not escaped by a backslash, so the first example
of '"quoted"' shouldn't have worked, yet it does.

It turns out that, under Windows, ruby.exe ignores argv and reparses it
from the raw command line. In rb_w32_cmdvector() in win32.c, the
section with the comment:

//
// if it's an input vector element and it's enclosed by quotes,
// we can remove them.
//

does just that.

This is important to me because of a project embedding Ruby, which
wasn't stripping quotes. I passed in a quoted filename and IO#foreach
failed. Yet the same script ran correctly with the same arguments when
called through ruby.exe.

Filenames under Windows cannon contain the double quote character, and
most of the Windows API functions work with filenames containing double
quotes by simply pretending they aren't there. (I'd say all the API
functions, but this *is* Windows, after all.) It would be nice if Ruby
under Windows was as tolerant.

Thanks again,

-- Timothy
 
T

Timothy Byrd

William said:
ruby.exe argvtest.rb '"quoted"'

ruby.exe argvtest.rb "\"quoted\""

Thanks. I didn't get this at first because my shell uses a caret (^)
as the escape character, not the backslash - backslash being the
directory separator on the PC.

After reading your reply, I realized I needed to go through the code to
figure out how Ruby is really behaving. For example, the MSVC runtime
strips out all quotes not escaped by a backslash, so the first example
of '"quoted"' shouldn't have worked, yet it does.

It turns out that, under Windows, ruby.exe ignores argv and reparses it
from the raw command line. In rb_w32_cmdvector() in win32.c, the
section with the comment:

//
// if it's an input vector element and it's enclosed by quotes,
// we can remove them.
//

does just that.

This is important to me because of a project embedding Ruby, which
wasn't stripping quotes. I passed in a quoted filename and IO#foreach
failed. Yet the same script ran with the same arguments when called
through ruby.exe.

Filenames under Windows cannon contain the double quote character, and
most of the Windows API functions work with filenames containing double
quotes by simply pretending they aren't there. (I'd say all the API
functions, but this *is* Windows, after all.) It would be nice if Ruby
under Windows was as tolerant.

Thanks again,

-- Timothy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top