Rubocop Lint / Void:在虚空环境中使用的文字

时间:2018-06-12 15:25:16

标签: ruby-on-rails ruby rubocop

我一直在运行rubocop并遇到 Lint / Void:在void上下文进攻中使用的文字。

以下代码:

routes_and_postcodes.each do |hash|
  2..7.each do |i|
    route = Route.where(name: hash[:route]).first if postcode.delete(" ").first(i) == hash[:postcode].delete(" ")
  end
end

我已阅读docs,但仍无法理解问题所在。 在上下文中,完整的代码在这里:

def choose_own_van_route(postcode)
route = nil
routes_and_postcodes = []
Route.all.each do |r|
  route_postcodes = r.postcode_array
  route_postcodes.each do |pc|
    routes_and_postcodes << { postcode: pc, route: r.name }
  end
end
routes_and_postcodes.each do |hash|
  2..7.each do |i|
    route = Route.where(name: hash[:route]).first if postcode.delete(" ").first(i) == hash[:postcode].delete(" ")
  end
end
route || Route.create(cut_off_time_mins_since_midnight: 0, name: "BLANK ROUTE HAD TO BE ASSIGNED")
end 

由于

1 个答案:

答案 0 :(得分:1)

2..7.each do |i|
  # ...
end

... 无效代码 !!你试过运行吗?您将看到以下错误:

NoMethodError: undefined method `each' for 7:Integer

要解决此问题,您需要在括号中定义Range

(2..7).each do |i|
  # ...
end

rubocop错误与此有关:由于 if each上定义了Integer方法(没有&#39; t,但是rubocop并不知道,然后代码才有效,但2..<something>范围定义没有任何意义;它是带有 void context 文字