Quick reference for efficient coding
This Ruby code snippet reads and processes the contents of two files, foreach_b.txt
and foreach_a.txt
. It works as follows:
foreach_b.txt
and iterates through each line in the file.foreach_b.txt
, it opens foreach_a.txt
and iterates through its lines.foreach_b.txt
with each line from foreach_a.txt
.foreach_a.txt
for the current line from foreach_b.txt
and moves on to the next line from foreach_b.txt
.Essentially, this code checks for common lines between the two files and reports each match.
File.foreach('foreach_b.txt') do |line1|
File.foreach('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.3.5