语法错误,意外 keyword_rescue,预期 keyword_end
syntax error, unexpected keyword_rescue, expecting keyword_end
我有以下 ruby 代码:
EmailTemplate.for(mailer).each do |template|
begin
print '.'
template.upload(publish)
rescue Mandrill::UnknownTemplateError
failed.push(mailer)
end
end
Rubocop 将我的代码更正为:
EmailTemplate.for(mailer).each do |template|
print '.'
template.upload(publish)
rescue Mandrill::UnknownTemplateError
failed.push(mailer)
end
现在 returns 出现以下错误:
syntax error, unexpected keyword_rescue, expecting keyword_end
我该如何解决?
Rubocop 警告是:
C: Style/RedundantBegin: Redundant begin block detected.
Ruby 2.5.0 添加了一个 feature:
rescue/else/ensure are now allowed to be used directly with do/end blocks. [Feature #12906]
但在那之前,是不允许的。所以会有语法错误。
让我们对sample.rb中的代码进行语法测试:
[].each do |a|
# ops
rescue Exception => ex
puts ex.inspect
end
来自终端:
Ruby$ ruby -c sample.rb
sample.rb:3: syntax error, unexpected keyword_rescue
rescue Exception => ex
^
sample.rb:5: syntax error, unexpected keyword_end, expecting end-of-input
Ruby$ rvm use 2.5.1
Using /Users/aruprakshit/.rvm/gems/ruby-2.5.1
Ruby$ ruby -c sample.rb
Syntax OK
参见News。所以在2.5.0之前,需要这样写:
[].each do |a|
begin
# ops
rescue => Exception
puts ex.inspect
end
end
您可以按照 Setting the target Ruby version.
将 Rubocop 配置为 select 您想要的 Ruby 版本
Some checks are dependent on the version of the Ruby interpreter which
the inspected code must run on. For example, enforcing using Ruby 2.3+
safe navigation operator rather than try can help make your code
shorter and more consistent... unless it must run on Ruby 2.2.
If .ruby-version exists in the directory RuboCop is invoked in,
RuboCop will use the version specified by it. Otherwise, users may let
RuboCop know the oldest version of Ruby which your project supports
with:
AllCops:
TargetRubyVersion: 2.4
出于某种原因,Rubocop 认为您是 运行 Ruby 2.5,而不是 Ruby 2.4.1。
您可以通过以下两种方式解决此问题:
1) 创建一个文件 .ruby-version
,内容为 2.4.1
。 Rubocop 应该从该文件中获取您的 Ruby 版本。
2) 将以下内容添加到您的 .rubocop.yml
:
AllCops:
TargetRubyVersion: 2.4
我有以下 ruby 代码:
EmailTemplate.for(mailer).each do |template|
begin
print '.'
template.upload(publish)
rescue Mandrill::UnknownTemplateError
failed.push(mailer)
end
end
Rubocop 将我的代码更正为:
EmailTemplate.for(mailer).each do |template|
print '.'
template.upload(publish)
rescue Mandrill::UnknownTemplateError
failed.push(mailer)
end
现在 returns 出现以下错误:
syntax error, unexpected keyword_rescue, expecting keyword_end
我该如何解决?
Rubocop 警告是:
C: Style/RedundantBegin: Redundant begin block detected.
Ruby 2.5.0 添加了一个 feature:
rescue/else/ensure are now allowed to be used directly with do/end blocks. [Feature #12906]
但在那之前,是不允许的。所以会有语法错误。
让我们对sample.rb中的代码进行语法测试:
[].each do |a|
# ops
rescue Exception => ex
puts ex.inspect
end
来自终端:
Ruby$ ruby -c sample.rb
sample.rb:3: syntax error, unexpected keyword_rescue
rescue Exception => ex
^
sample.rb:5: syntax error, unexpected keyword_end, expecting end-of-input
Ruby$ rvm use 2.5.1
Using /Users/aruprakshit/.rvm/gems/ruby-2.5.1
Ruby$ ruby -c sample.rb
Syntax OK
参见News。所以在2.5.0之前,需要这样写:
[].each do |a|
begin
# ops
rescue => Exception
puts ex.inspect
end
end
您可以按照 Setting the target Ruby version.
将 Rubocop 配置为 select 您想要的 Ruby 版本Some checks are dependent on the version of the Ruby interpreter which the inspected code must run on. For example, enforcing using Ruby 2.3+ safe navigation operator rather than try can help make your code shorter and more consistent... unless it must run on Ruby 2.2.
If .ruby-version exists in the directory RuboCop is invoked in, RuboCop will use the version specified by it. Otherwise, users may let RuboCop know the oldest version of Ruby which your project supports with:
AllCops:
TargetRubyVersion: 2.4
出于某种原因,Rubocop 认为您是 运行 Ruby 2.5,而不是 Ruby 2.4.1。
您可以通过以下两种方式解决此问题:
1) 创建一个文件 .ruby-version
,内容为 2.4.1
。 Rubocop 应该从该文件中获取您的 Ruby 版本。
2) 将以下内容添加到您的 .rubocop.yml
:
AllCops:
TargetRubyVersion: 2.4