Best way to convert path slash for backtick usage?

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?

  2. How to determine the applications Operating System environment?

This is the function I need it for

def create_command_line_string(cmd)
result = Rails.root.join(cmd)
logger.debug("@@@@ - #{result}")
result
end

Any ideas or pointers would be greatly appreciated

James W. 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?

  2. How to determine the applications Operating System environment?

This is the function I need it for

def create_command_line_string(cmd)
result = Rails.root.join(cmd)
logger.debug("@@@@ - #{result}")
result
end

Any ideas or pointers would be greatly appreciated

Just an adendum to this.
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 fine on the server as the Rails.root will give me the correct
application root path but not on development.
In development I obviously need to just replace the / slashes with \
slashes.
It would be nice though if the ` backtick usage actually returned
something. Anything at all would be good but the output from the
logger.debug looks like this

@@@@ Create domain response = , Exit code =

So maybe backticks are not the right solution? Is there something that
will give me more info over what is going on so I can deal with results
more effectively?

James W. 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
[email protected]

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

Markus R. wrote:

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

File.join does exactly the same thing as Rails.root.join so that’s no
good at all. But thanks anyway. It was worth a thought.

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”

Marnen Laibow-Koser wrote:

James W. 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
[email protected]

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.

Aldric G. 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:

On Sep 4, 2009, at 12:40 PM, James W. wrote:

tries to give you a ''

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:

irb> PLATFORM
=> “universal-darwin9.0”
irb> File::SEPARATOR
=> “/”
irb> 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(‘/’,‘\\’)

irb> Dir.pwd
=> “/Users/rab/code/ruby”
irb> Dir.pwd.gsub(‘/’,‘\\’)
=> “\Users\rab\code\ruby”
irb> puts _
\Users\rab\code\ruby
=> nil

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

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

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

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

PLATFORM
=> “i386-mswin32”

Thank you :slight_smile:

irb> PLATFORM
=> “universal-darwin9.0”
irb> File::SEPARATOR
=> “/”
irb> 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.