Challenge: need to iterate over a set of patient records…within the set, each patient has several records… so when the initial iteration gets to a new patient, the code needs to include a sub-iteration specific to the patient. For example, a patient’s subset of data includes their location & treatment history. We want to capture the patient’s last location, but their first instance of treatment. The data is extracted into a patient-specific data hash.
patients.each_with_index do |row, index|
i = index
until patients[i]['ID'] != patients[index]['ID']
i += 1
break if !patients[i] # no more records
Takeaway: this could benefit from refactoring…
- The sub-iteration repeats the processing of records due to reference to the
rowfor data capture, instead of
- Could the initial iteration’s
indexbe reset/moved to the new patient once the sub-iteration is finished?