Heads up: This description was created by AI and might not be 100% accurate.
foreach.rb
This Ruby code snippet demonstrates nested file reading to compare each line of foreach_b.txt
with every line in foreach_a.txt
. It prints “MATCHED: [line1]” if a match is found, and exits the inner loop. The break
statement ensures that only the first match for each line in foreach_b.txt
is printed. The #=> nil
indicates that no further output occurred after the initial matches were found, essentially meaning the loops completed without any additional matches being reported.
Ruby code snippet
File.foreach('input/file/foreach_b.txt') do |line1|
File.foreach('input/file/foreach_a.txt') do |line2|
if line1.eql?(line2)
puts("MATCHED: #{line1}")
break
end
end
end
MATCHED: 9
MATCHED: 2
MATCHED: 6
#=> nil
Executed with Ruby 3.4.5
.