quote stripping in windows ruby.exe command line

Discussion in 'Ruby' started by Timothy Byrd, Oct 2, 2005.

  1. Timothy Byrd

    Timothy Byrd Guest

    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
     
    Timothy Byrd, Oct 2, 2005
    #1
    1. Advertising

  2. Timothy Byrd wrote:
    > 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\""
     
    William James, Oct 2, 2005
    #2
    1. Advertising

  3. On 10/2/05, Timothy Byrd <> wrote:
    > 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
    --
    Austin Ziegler *
    * Alternate:
     
    Austin Ziegler, Oct 2, 2005
    #3
  4. Timothy Byrd

    Timothy Byrd Guest

    [hoping this isn't a double post]

    William James wrote:

    > 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
     
    Timothy Byrd, Oct 3, 2005
    #4
  5. Timothy Byrd

    Timothy Byrd Guest

    William James wrote:

    > 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
     
    Timothy Byrd, Oct 3, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    13,376
  2. Stimp
    Replies:
    2
    Views:
    2,309
    Stimp
    Sep 20, 2006
  3. Eric Layman
    Replies:
    3
    Views:
    652
    Rad [Visual C# MVP]
    Apr 14, 2007
  4. Replies:
    6
    Views:
    454
    Stefan Ram
    Jun 15, 2008
  5. Valery Khamenya
    Replies:
    3
    Views:
    738
Loading...

Share This Page