【发布时间】:2016-05-23 10:53:28
【问题描述】:
我有以下方法:
def fetch_something
@fetch_something ||= array_of_names.inject({}) do |results, name|
begin
results[name] = fetch_value!(name)
rescue NoMethodError, RuntimeError
next
end
results
end
end
为了它的目的:它为给定的name 获取一个可能引发错误的值,在这种情况下它将忽略name 并尝试下一个。
虽然这工作正常,但我从 Rubocop 收到一条错误消息:
Lint/NextWithoutAccumulator:将 next 与累加器参数一起使用 减少。
谷歌搜索该错误导致我到http://www.rubydoc.info/gems/rubocop/0.36.0/RuboCop/Cop/Lint/NextWithoutAccumulator,它说不要省略累加器,这将导致方法如下所示:
def fetch_something
@fetch_something ||= array_of_names.inject({}) do |results, name|
begin
results[name] = fetch_value!(name)
rescue NoMethodError, RuntimeError
next(name)
end
results
end
end
问题是,这个改变打破了原本的工作方法。关于如何解决这个问题的任何想法?
更新:示范示例:array_of_names = ['name1','name2','name3']
def fetch_value!(name)
# some code that raises an error if the name doesn't correspond to anything
end
fetch_something
# => {'name1' => {key1: 'value1', ...}, 'name3' => {key3: 'value3', ...}}
# 'name2' is missing since it wasn't found durcing the lookup
【问题讨论】:
-
试试
next(results)怎么样? -
@user12341234 这也破坏了它。
-
也许您可以显示示例输入/输出来衡量预期的行为?
-
@user12341234 添加示例
-
Severin,请参阅@user12341234 答案。我不知道,结果哈希中不需要没有值的键。
标签: ruby-on-rails ruby loops ruby-on-rails-4 rubocop