繁体   English   中英

Rubocop Lint / Void:在无效上下文中使用文字

[英]Rubocop Lint/Void: Literal used in void context

我一直在运行rubocop,并且遇到了Lint / Void:Literal在无效上下文犯罪中使用的情况

以下代码:

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

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

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 

谢谢

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

...是无效代码 您是否尝试过运行它? 您会看到以下错误:

NoMethodError: undefined method `each' for 7:Integer

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

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

rubocop错误与此有关:由于如果Integer定义了each方法(没有,但是rubocop不知道),那么代码将是有效的,但是2..<something>范围定义毫无用处; 这是带有无效上下文文字

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM