Ruby 只执行第一行?

Ruby only executing the first line?

我正在编写 ruby 脚本并发现了这个奇怪的行为。

使用ruby 2.4.2 [x86_64-darwin16]

基本上我试图回显两条分开的消息,在我的 index.rb 文件中我得到:

exec("echo 'teste'")
exec("echo 'teste2'")

但是当我运行ruby ./index.rb

输出为:

teste

为什么会这样?

这不应该是输出吗?

testeteste2

exec([env,] command... [,options])

Replaces the current process by running the given external command docs

意思是第一次调用exec把你的ruby程序替换成echo,所以ruby程序的其余部分不会执行。

您可以使用反引号 运行 您想要的命令:

`echo 'teste'`
`echo 'teste2'`