我有以下方法:紅寶石:在循環跳過元素,如果將引發異常
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一個錯誤,指出:
棉絨/ NextWithoutAccumulator:下次使用與 蓄能器參數一個減少。
谷歌搜索的錯誤使我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