Hello everyone,
Unix-like operating systems use a colon to separate each directory in the PATH, while Windows uses a semicolon.
The question is: is there some class in the Ruby library that knows about this difference?
Or do I have to resort to something like the following?
if RUBY_PLATFORM =~ /(win|w)32$/
# ...
end
I'm asking this question because I need to figure out if a given executable is in the path and, if it does, retrieve its full path.
So I whipped up this one-liner which seems to work as expected (except on Windows, of course):
ENV["PATH"].split(":").collect {|p| File.join(p, "foo")}.keep_if{|p| File.exist? p}.first
where "foo" is the name of the executable.
While we're at it, can someone think of a better / more idiomatic way to do it?
Obviously, simply trying to run the executable is not an option.
Thanks in advance.
···
--
Stefano
Hello everyone,
Unix-like operating systems use a colon to separate each directory in
the PATH, while Windows uses a semicolon.
The question is: is there some class in the Ruby library that knows
about this difference?
File::PATH_SEPARATOR
I'm asking this question because I need to figure out if a given
executable is in the path and, if it does, retrieve its full path.
require 'ptools'
File.which('ls') # => '/bin/ls'
Works on Windows, too, with or without an extension:
File.which('notepad') # Works
File.which('notepad.exe') # Also works
Regards,
Dan
···
On Oct 19, 2:02 pm, Stefano Mioli <stefano.mi...@gmail.com> wrote:
Many thanks, Dan.
I didn't know about ptools, and somehow I just saw File::SEPARATOR,
but not File::PATH_SEPARATOR.
···
On Tue, Oct 19, 2010 at 10:08 PM, Daniel Berger <djberg96@gmail.com> wrote:
On Oct 19, 2:02 pm, Stefano Mioli <stefano.mi...@gmail.com> wrote:
The question is: is there some class in the Ruby library that knows
about this difference?
File::PATH_SEPARATOR
I'm asking this question because I need to figure out if a given
executable is in the path and, if it does, retrieve its full path.
require 'ptools'
File.which('ls') # => '/bin/ls'
Works on Windows, too, with or without an extension:
File.which('notepad') # Works
File.which('notepad.exe') # Also works
--
Stefano
string#split accept a regular expression
path_list = ENV['PATH'].split(/[:;]/) # work s on Linus and Windows
HTH gfb
"Stefano Mioli" <stefano.mioli@gmail.com> wrote in message
news:AANLkTikcTzTvNbejW6JEL9_tmBAvKBqer7_W1X6p20M_@mail.gmail.com ...
···
On Tue, Oct 19, 2010 at 10:08 PM, Daniel Berger <djberg96@gmail.com> wrote:
On Oct 19, 2:02 pm, Stefano Mioli <stefano.mi...@gmail.com> wrote:
The question is: is there some class in the Ruby library that knows
about this difference?
File::PATH_SEPARATOR
I'm asking this question because I need to figure out if a given
executable is in the path and, if it does, retrieve its full path.
require 'ptools'
File.which('ls') # => '/bin/ls'
Works on Windows, too, with or without an extension:
File.which('notepad') # Works
File.which('notepad.exe') # Also works
Many thanks, Dan.
I didn't know about ptools, and somehow I just saw File::SEPARATOR,
but not File::PATH_SEPARATOR.
--
Stefano
Robert_K1
(Robert K.)
20 October 2010 08:35
5
Well, it works. But it isn't helpful.
irb(main):002:0> 'C:\\foo;W::\\bar'.split(/[:;]/)
=> ["C", "\\foo", "W", "", "\\bar"]
Cheers
robert
···
On Wed, Oct 20, 2010 at 9:20 AM, GianFranco Bozzetti <gf.bozzetti@alice.it> wrote:
string#split accept a regular expression
path_list = ENV['PATH'].split(/[:;]/) # work s on Linus and Windows
HTH gfb
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/