Suppressing the output of a command run using 'system' method while running it in a ruby script

后端 未结 8 1289
醉梦人生
醉梦人生 2020-12-29 20:13

I am not sure if this makes sense but I am thinking if there is a way to suppress the output shown for a command when run using the system method in ruby? I mea

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 21:10

    • define null_device, it's operating systems dependent: windows 7 and newer use nul, while *nix systems uses /dev/null
    • run the command
    • redirect it's standard and error output to null_device
    • then use the exit code, or the backtick method as mentioned by @mikej to determine the output

    as follows:

     null_device = Gem.win_platform? ? "/nul" : "/dev/null"
    
     do method
       system "run command 1>#{null_device} 2>#{null_device} "
       p ($? == 0)
     end
    

提交回复
热议问题