A small Ruby service builds a hash of fields for a report from a sequence of strings. After a recent change, some inputs started producing an empty result or skipping the last elements.
Requirements
- Use Ruby
3.3.
- Only the standard library is allowed in the code.
- The program runs as a regular script:
ruby lib/report_mapper.rb.
- The API stays the same:
ReportMapper#build(rows, prefix:) accepts an enumerable object and a string prefix, and returns a Hash.
- The input may be either an
Array or an Enumerator.
- The traversal order of the input must be preserved.
- If an error occurs for one element, processing must stop with the exception propagated, without suppression.
Reproduction
- Pass a regular array of strings to
build.
- Pass a one-shot
Enumerator to build that has already been partially read before the call.
- Call
build again on the same enumeration object.
Expected behavior
- For each input element, the key should be returned in the form
"#{prefix}:#{value}".
- Arrays and enumerators must be processed identically in terms of the result.
- The same input should not lose elements between calls if the enumeration object itself has not yet been exhausted.
- If an exception occurs during processing, the same exception must be raised outward.