Ruby Quick Reference

Quick reference for efficient coding

View the Project on GitHub YumaYX/RubyQuickReference

foreach.rb(file)

This Ruby code snippet reads and processes the contents of two files, foreach_b.txt and foreach_a.txt. It works as follows:

  1. It opens foreach_b.txt and iterates through each line in the file.
  2. For each line from foreach_b.txt, it opens foreach_a.txt and iterates through its lines.
  3. During this nested iteration, it compares the current line from foreach_b.txt with each line from foreach_a.txt.
  4. If it finds a matching line in both files (i.e., the lines are identical), it prints a message indicating that a match was found, including the matched line.
  5. Once a match is found, it stops checking further lines in 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.

Execution:

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