puts "start"
ret1 = exec('pwd')
puts ret1
ret2= exec('hostname')
puts ret2
a = "."
puts a
exec('ls ~')
////code exit from here... not any other output why?
puts a
puts a
puts a
我的代码在第二次调用exec后退出。那是为什么?
% ruby exec.rb
start
/Users/xxx/code/
这是我运行此代码时的输出。
答案 0 :(得分:2)
Kernel#exec
替换当前正在运行的进程。一旦执行,代码的剩余部分就不会运行。
puts "start"
ret1 = exec('pwd') # <---- After this, no more remaining code is executed.
...
如果要获取命令的输出,请改为使用Kernel#`
:
puts "start"
ret1 = `pwd`