Saari Development

Ruby: Searching for a variable string using grep

Posted in Uncategorized by imsaar on April 29th, 2006

I was looking for a way to search for a variable string inside a ruby method and I think I have found _a_ solution.

def print_line_containing(file, str)  File.open(file).grep(/#{str}/).each do |line| puts "#{line}" endend

def print_line_containing(file, str1, str2)  File.open(file).grep(/#{str1}\s+#{str2}/).each do |line| puts "#{line}" endend