Saari Development

Ali Rizvi's Technical Blog as a Professional Software Development Engineer

Ruby: Searching for a variable string using grep

leave a comment »

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}" end
end

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

Written by imsaar

April 29, 2006 at 1:52 am

Posted in general

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: