Rubocop Lint/Void:在无效上下文中使用的文字
Rubocop Lint/Void: Literal used in void context
我是 运行 rubocop 并遇到了 Lint/Void:文字用于 void context 罪行。
以下代码:
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
谢谢
2..7.each do |i|
# ...
end
...是无效代码!!你试过运行了吗?您会看到以下错误:
NoMethodError: undefined method `each' for 7:Integer
要解决此问题,您需要在方括号中定义 Range
:
(2..7).each do |i|
# ...
end
rubocop
错误与此有关:因为 if 在 Integer
上定义了一个 each
方法(没有' t,但 rubocop
不知道),那么代码将是有效的,但 2..<something>
范围定义将毫无用处;这是一个 文字,带有 void 上下文。
我是 运行 rubocop 并遇到了 Lint/Void:文字用于 void context 罪行。
以下代码:
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
谢谢
2..7.each do |i|
# ...
end
...是无效代码!!你试过运行了吗?您会看到以下错误:
NoMethodError: undefined method `each' for 7:Integer
要解决此问题,您需要在方括号中定义 Range
:
(2..7).each do |i|
# ...
end
rubocop
错误与此有关:因为 if 在 Integer
上定义了一个 each
方法(没有' t,但 rubocop
不知道),那么代码将是有效的,但 2..<something>
范围定义将毫无用处;这是一个 文字,带有 void 上下文。