gpt4 book ai didi

ruby-on-rails - Rubocop Lint /空 : Literal used in void context

转载 作者:行者123 更新时间:2023-12-04 01:55:44 25 4
gpt4 key购买 nike

我一直在运行 rubocop 并遇到了 Lint/Void: Literal used in void context offence。

以下代码:

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错误与此有关:因为如果有 eachInteger 上定义的方法(没有,但 rubocop 不知道),那么代码将是有效的,但 2..<something>范围定义没有任何意义;它是一个带有空上下文的文字。

关于ruby-on-rails - Rubocop Lint /空 : Literal used in void context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50820735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com