Rubocop:method 行数过多
Rubocop:method has too many lines
您好,我是 ruby 编程新手。
运行 在我的项目中进行 rubocop 检查,它说:
Method has too many lines. [13/10] def refresh_status
这是我的方法:
def refresh_status
lost = false
in_progress = false
won = false
@bets.each do |bet|
lost = true if bet.result == :lost
if bet.result == :canceled
@to_return /= bet.odd
won = true
end
in_progress = true if bet.result == :in_progress
won = true if bet.result == :won
end
def_result_after_refresh(lost, in_progress, won)
end
def def_result_after_refresh(lost, in_progress, won)
if lost
@result = :lost
elsif in_progress
@result = :in_progress
elsif won
@result = :won
end
end
找不到缩短该方法的方法,也许您可以帮忙?
您可以使用一些 Enumerable
方法。
def refresh_status
@to_return /= @bets.select { |bet| bet.result == :canceled }.map(&:odd).reduce(1, :*)
results = @bets.map { |bet| bet.result == :cancelled ? :won : bet.result }.uniq
@result = case
when results.include?(:lost) then :lost
when results.include?(:in_progress ) then :in_progress
when results.include?(:won) then :won
end
end
您好,我是 ruby 编程新手。 运行 在我的项目中进行 rubocop 检查,它说:
Method has too many lines. [13/10] def refresh_status
这是我的方法:
def refresh_status
lost = false
in_progress = false
won = false
@bets.each do |bet|
lost = true if bet.result == :lost
if bet.result == :canceled
@to_return /= bet.odd
won = true
end
in_progress = true if bet.result == :in_progress
won = true if bet.result == :won
end
def_result_after_refresh(lost, in_progress, won)
end
def def_result_after_refresh(lost, in_progress, won)
if lost
@result = :lost
elsif in_progress
@result = :in_progress
elsif won
@result = :won
end
end
找不到缩短该方法的方法,也许您可以帮忙?
您可以使用一些 Enumerable
方法。
def refresh_status
@to_return /= @bets.select { |bet| bet.result == :canceled }.map(&:odd).reduce(1, :*)
results = @bets.map { |bet| bet.result == :cancelled ? :won : bet.result }.uniq
@result = case
when results.include?(:lost) then :lost
when results.include?(:in_progress ) then :in_progress
when results.include?(:won) then :won
end
end