From: Stefano Crocco Date: 2008-06-08T23:23:17+09:00 Subject: Re: Search File Contents On Sunday 08 June 2008, Andy Dbest wrote: > Hi Guys, > > I have a small code block that is frustrating me for some time now. I > want to search for some text in a file. Simple isnt it? > > def read_file week_num > week_num = week_num.to_s > f = File.open("fixtures.txt", "r") > lines = f.readlines > #week_num = '1.' > lines.each do|line| > if line.chomp.eql? week_num > puts "Found the week number" > end > end > f.close > end > > The above is the method, and I am searching for "1.". I am passing the > variable "week_num" and i even tried to convert it to string. The search > code as it is right now, does not output "Found the week number", even > though the file contains "1." > > If i uncomment the line that assigns week_num to '1.' directly, then it > works. What am i doing wrong? If assiging week_num a value inside the method makes the code work, I'd say the problem is in the argument which is passed to the method. Have you tried to put a puts week_num at the beginning of the method, to see whether the argument you get is the one you expect? Stefano