Those are great solutions, but what if you want to find all occurrences of a substring? That’s where scan comes in!
matches = varMessage.scan(/balaji\.so/)
puts "Found #{matches.count} occurrences." if matches.any?
Or, if you’re checking multiple lines of a file stored in an array, grep is useful.
lines = varMessage.split("\n")
matches = lines.grep(/balaji\.so/)
puts "Matched lines: #{matches}"
This is great for cases where you need to filter out only the lines containing a specific substring.