Best way to convert path slash for backtick usage?

James West wrote:

Hi, Depenbding on whether running under windows or unix/mac I obviously need to use the \ instead of the / character for shell commands so I have 2 questions really 1) I would love to know if anyone has a neat way of dealing with converting paths to use '\' instead of '/' for use with command line calls like command and backtick?

path.gsub('/', '\\')

Actually, you may need 4 backslashes instead of 2; Ruby does weird thing with backslashes. But that should get you going.

Best,

Depenbding on whether running under windows or unix/mac I obviously need to use the \ instead of the / character for shell commands so I have 2 questions really 1) I would love to know if anyone has a neat way of dealing with converting paths to use '\' instead of '/' for use with command line calls like command and backtick?

Split it into segments and use File.join()...that's what it's there for.

-- MarkusQ

Marnen Laibow-Koser wrote:

James West wrote:

Hi, Depenbding on whether running under windows or unix/mac I obviously need to use the \ instead of the / character for shell commands so I have 2 questions really 1) I would love to know if anyone has a neat way of dealing with converting paths to use '\' instead of '/' for use with command line calls like command and backtick?

path.gsub('/', '\\')

Actually, you may need 4 backslashes instead of 2; Ruby does weird thing with backslashes. But that should get you going.

Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org

path.gsub would do the job. Thankyou. I need to know how to determine the current operating system in order to decide whether or not to call the gsub routine. Any ideas?

I'm thinking that Rails must have a way of dealing with this otherwise it wouldn't run on Windows.

In Windows:

Dir.chdir 'c:/myscripts'

=> 0

Dir.pwd

=> "c:/myscripts"

I think you're making a mountain out of a molehill. Ruby interprets the path properly. Keep it to '/' and do input sanitation when the user tries to give you a '\'

Yet, I don't know what you're doing, so..

PLATFORM

=> "i386-mswin32"

Aldric Giacomoni wrote:

In Windows:

Dir.chdir 'c:/myscripts'

=> 0

Dir.pwd

=> "c:/myscripts"

I think you're making a mountain out of a molehill. Ruby interprets the path properly. Keep it to '/' and do input sanitation when the user tries to give you a '\'

Yet, I don't know what you're doing, so..

No mountain, no molehill :slight_smile: What I'm doing was clearly (I hope) explained in my 2 opening posts but just to be clear here it is again

I'm actually trying to call cap deploy with the result of this function

   logger.debug("Creating domain")    cmd = create_command_line_string("cap cloud_deploy:setup -S destination=#{self.requested_url}")    response = `#{cmd}`    exit_code = $?    logger.debug("@@@@ Create domain response = #{response}, Exit code = #{exit_code}")    exit_code

This is producing a command "c:/development/cloud/cap cloud_deploy:setup -S etc..." Which is wrong on Windows :slight_smile:

PLATFORM

=> "i386-mswin32"

Thank you :slight_smile: --

PLATFORM

=> "universal-darwin9.0"

File::SEPARATOR

=> "/"

File::ALT_SEPARATOR

=> nil

Can you use either of these on Windows to construct a suitable gsub?

Guessing:    cmd.gsub!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR

Or you can go brute-force with: gsub('/','\\\\')

Dir.pwd

=> "/Users/rab/code/ruby"

Dir.pwd.gsub('/','\\\\')

=> "\\Users\\rab\\code\\ruby"

puts _

\Users\rab\code\ruby => nil

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

Guessing:    cmd.gsub!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

You absolute star :slight_smile: That will do EXACTLY what I need. Thanks so much. James

PLATFORM

=> "i386-mswin32"

Thank you :slight_smile: --

> PLATFORM => "universal-darwin9.0" > File::SEPARATOR => "/" > File::ALT_SEPARATOR => nil

irb(main):060:0> RUBY_DESCRIPTION => "ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-mswin32]" irb(main):058:0> File::SEPARATOR => "/" irb(main):059:0> File::ALT_SEPARATOR => "\\"

Hope that helps.