gpt4 book ai didi

ruby - 为什么 ruby​​ 代码在我第二次调用 exec 后崩溃/退出?

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:22 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com