Stop processing if found

Hello experts.

I am trying to scan through a text file and look for a matching VIN, if
its found I don’t want to continue processing. So far the code below
works however how do I stop processing the rest of the code in my script
if a match is found ?


File.readlines(file).each do |line|
print line if (line[vin])

you can write “break” to exit a loop:

File.readlines(file).each do |line|
if (line[vin])
print line

Actually, a ‘break’ is not necessary here:

found = File.readlines(file).first {|line| line[vin] }
print found if found

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs