[英]Rubocop Lint/Void: Literal used in void context
I have been running rubocop and encountered a Lint/Void: Literal used in void context offence. 我一直在运行rubocop,并且遇到了Lint / Void:Literal在无效上下文犯罪中使用的情况 。
the following code: 以下代码:
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
I have read the docs , but still cant understand what the issue is. 我已经阅读了文档 ,但仍然无法理解问题所在。 In context, the full code is here: 在上下文中,完整的代码在这里:
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
Thanks 谢谢
2..7.each do |i|
# ...
end
...is invalid code !! ...是无效代码 ! Have you tried running it? 您是否尝试过运行它? You'll see the following error: 您会看到以下错误:
NoMethodError: undefined method `each' for 7:Integer
To fix this, you need to define the Range
in brackets: 要解决此问题,您需要在方括号中定义Range
:
(2..7).each do |i|
# ...
end
The rubocop
error is in relation to this: Since if there were an each
method defined on Integer
(there isn't, but rubocop
doesn't know that), then the code would be valid but the 2..<something>
range definition would serve no purpose; rubocop
错误与此有关:由于如果在Integer
定义了each
方法(没有,但是rubocop
不知道),那么代码将是有效的,但是2..<something>
范围定义毫无用处; it's a literal with a void context . 这是带有无效上下文的文字 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.