“which in ruby”: Checking if program exists in $PATH from ruby

后端 未结 16 1121
萌比男神i
萌比男神i 2020-12-07 12:15

my scripts rely heavily on external programs and scripts. I need to be sure that a program I need to call exists. Manually, I\'d check this using \'which\' in the commandlin

16条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 12:38

    for jruby, any of the solutions that depend on mkmf may not work, as it has a C extension.

    for jruby, the following is an easy way to check if something is executable on the path:

    main » unix_process = java.lang.Runtime.getRuntime().exec("git status")
    => #
    main » unix_process.exitValue()
    => 0
    main »
    

    if the executable isn't there, it will raise a runtime error, so you may want to do this in a try/catch block in your actual usage.

提交回复
热议问题