Skip to content

Files

Latest commit

 

History

History
58 lines (45 loc) · 1020 Bytes

Style-CombinableLoops.md

File metadata and controls

58 lines (45 loc) · 1020 Bytes

Pattern: Use of uncombined loops

Issue: -

Description

This rule checks for places where multiple consecutive loops over the same data can be combined into a single loop. It is very likely that combining them will make the code more efficient and more concise.

It is marked as unsafe, because the first loop might modify a state that the second loop depends on; these two aren't combinable.

Examples

# bad
def method
  items.each do |item|
    do_something(item)
  end

  items.each do |item|
    do_something_else(item)
  end
end

# good
def method
  items.each do |item|
    do_something(item)
    do_something_else(item)
  end
end

# bad
def method
  for item in items do
    do_something(item)
  end

  for item in items do
    do_something_else(item)
  end
end

# good
def method
  for item in items do
    do_something(item)
    do_something_else(item)
  end
end

Further Reading