[译] Ruby 2.6 Kernel 的system 方法增加是否抛出异常参数。

本博客系列翻译自 Bigbinary 的 Ruby 2.6 系列, 已得到作者允许。Ruby 2.6.0-preview2 现已发布

平时我们会写一些自动化设置和部署 Rails 应用的脚本。在这些脚本中,我们会时常使用系统命令,例如 bundle install,抑或是 rake db:create等等。

我们假设一下:我们需要在一个Rails 项目里头跑 rake db:migrate,我们可以使用 Kernel#system 方法。

irb> system('rake db:migrate')  

Ruby 2.5.0

执行system方法会返回 true 或者 false(Ruby 2.5 下)。system 其中一个Feature(థ౪థ) 就是他会把异常抛出给吃掉。

我们假设这个命令会成功,那么我们会得到返回值 true

irb> system('rake db:migrate')  
 => true

那我们继续假如:这个migration 尝试加一个字段到一个不存在的表里头。这个情况下,我们会得到返回值 false

irb> system('rake db:migrate')  
== 20180311211836 AddFirstNameToAdmins: migrating =============================
-- add_column(:admins, :first_name, :string)
rake aborted!  
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "admins" does not exist  
: ALTER TABLE "admins" ADD "first_name" character varying
.
.
Tasks: TOP => db:migrate  
(See full trace by running task with --trace)
 => false

如我们所见,即使执行系统命令失败,方法返回值还是会是 false。Ruby 不会因此抛出异常。尽管如此,我们可以用这个方法来抛出异常。

irb> system('rake db:migrate') || raise('Failed to run migrations')  
== 20180311211836 AddFirstNameToAdmins: migrating =============================
-- add_column(:admins, :first_name, :string)
rake aborted!  
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "admins" does not exist  
: ALTER TABLE "admins" ADD "first_name" character varying
.
.
.
Tasks: TOP => db:migrate  
(See full trace by running task with --trace)
Traceback (most recent call last):  
        2: from /Users/amit/.rvm/rubies/ruby-2.5.0/bin/irb:11:in `<main>'
        1: from (irb):4
RuntimeError (Failed to run migrations)  

Ruby 2.6.0-preview1

Ruby 2.6 给 system 方法提供了一个新的option, 也就是 exception ,你可以通过设置 exception: true 来让方法可以抛出异常。

irb> system('rake db:migrate', exception: true)  
== 20180311211836 AddFirstNameToAdmins: migrating =============================
-- add_column(:admins, :first_name, :string)
rake aborted!  
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "admins" does not exist  
: ALTER TABLE "admins" ADD "first_name" character varying
.
.
.
Tasks: TOP => db:migrate  
(See full trace by running task with --trace)
Traceback (most recent call last):  
        3: from /Users/amit/.rvm/rubies/ruby-2.6.0-preview1/bin/irb:11:in `<main>'
        2: from (irb):2
        1: from (irb):2:in `system'
RuntimeError (Command failed with exit 1: rake)  

当然,你只要设置exception 为false,那么这个函数的行为就和以前一样,返回布尔值。

irb> system('rake db:migrate', exception: false)  
== 20180311211836 AddFirstNameToAdmins: migrating =============================
-- add_column(:admins, :first_name, :string)
rake aborted!  
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "admins" does not exist  
: ALTER TABLE "admins" ADD "first_name" character varying
.
.
.
Tasks: TOP => db:migrate  
(See full trace by running task with --trace)
 => false

这里是相关的commit讨论

system 也不是唯一可以执行这些语句的方法,我(作者)六年前写了一个博客讨论这些(执行系统命令)的方法的异同:backtick, exec, sh, popen3, popen2e and Process.spawn.

原文地址