Separator in $PATH

Discussion in 'Ruby' started by Stefano Mioli, Oct 19, 2010.

  1. 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
     
    Stefano Mioli, Oct 19, 2010
    #1
    1. Advertising

  2. On Oct 19, 2:02=A0pm, Stefano Mioli <> wrote:
    > Hello everyone,
    > =A0 =A0 =A0 =A0 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') # =3D> '/bin/ls'

    Works on Windows, too, with or without an extension:

    File.which('notepad') # Works
    File.which('notepad.exe') # Also works

    Regards,

    Dan
     
    Daniel Berger, Oct 19, 2010
    #2
    1. Advertising

  3. On Tue, Oct 19, 2010 at 10:08 PM, Daniel Berger <> wrote:
    >
    > On Oct 19, 2:02=A0pm, Stefano Mioli <> 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') # =3D> '/bin/ls'
    >
    > Works on Windows, too, with or without an extension:
    >
    > File.which('notepad') =A0 =A0 # 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.

    --=20
    Stefano
     
    Stefano Mioli, Oct 19, 2010
    #3
  4. On Wed, Oct 20, 2010 at 9:20 AM, GianFranco Bozzetti
    <> wrote:
    > string#split accept a regular expression
    >
    > path_list = ENV['PATH'].split(/[:;]/) # work s on Linus and Windows
    >
    > HTH gfb


    Well, it works. But it isn't helpful.

    irb(main):002:0> 'C:\\foo;W::\\bar'.split(/[:;]/)
    => ["C", "\\foo", "W", "", "\\bar"]

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Oct 20, 2010
    #4
    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. Stephen Ferg
    Replies:
    30
    Views:
    960
    Andrew Dalke
    Sep 30, 2003
  2. Tony Meyer
    Replies:
    1
    Views:
    640
    Stephen Horne
    Sep 26, 2003
  3. swapnil

    python path separator

    swapnil, Sep 2, 2010, in forum: Python
    Replies:
    3
    Views:
    1,795
    Nobody
    Sep 2, 2010
  4. Justin Johnson

    Path Separator and Windows

    Justin Johnson, Dec 29, 2005, in forum: Ruby
    Replies:
    19
    Views:
    383
    Justin Johnson
    Dec 30, 2005
  5. Matt Garrish
    Replies:
    9
    Views:
    171
    Matt Garrish
    Jan 17, 2006
Loading...

Share This Page