Skip to content

Latest commit

 

History

History
24 lines (18 loc) · 403 Bytes

replace-loop-with-pipeline.md

File metadata and controls

24 lines (18 loc) · 403 Bytes

Replace Loop With Pipeline

Separate data transformation from side effects by replacing an imperative loop with a sequence of functional operations.

Example

Before

sessions.each do |token|
  if session.token != current_session.token
    session.token.expire!
  end
end

After

sessions.map(&:token)
  .reject { |t| t == current_session.token }
  .each(&:expire!)