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/
这是我运行这段代码时的输出。
最佳答案
Kernel#exec
替换当前运行的进程。一旦执行,代码的剩余部分将不会运行。
puts "start"
ret1 = exec('pwd') # <---- After this, no more remaining code is executed.
...
如果你想得到命令的输出,使用Kernel#`
相反:
puts "start"
ret1 = `pwd`
关于ruby - 为什么 ruby 代码在我第二次调用 exec 后崩溃/退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194564/