# Search File Contents

**URL:** <https://www.ruby-forum.com/t/search-file-contents/139873>\
**Category:** Ruby\
**Created:** [June 8, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/search-file-contents/139873 "2008-06-08T16:07:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aca169/32.png) [@dbest](https://www.ruby-forum.com/u/dbest)\
**Post date:** [June 8, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/search-file-contents/139873/1 "2008-06-08T16:07:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dbest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aca169/32.png) [@dbest](https://www.ruby-forum.com/u/dbest)\
**Post date:** [June 8, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/search-file-contents/139873/2 "2008-06-08T16:25:34Z")

</div>

On Sunday 08 June 2008, Andy Dbest wrote:

> lines.each do|line|  
> 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

---

<div class="post-metadata">

**Author:** ![dbest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aca169/32.png) [@dbest](https://www.ruby-forum.com/u/dbest)\
**Post date:** [June 8, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/search-file-contents/139873/3 "2008-06-08T16:29:10Z")

</div>

Stefano C. wrote:

> On Sunday 08 June 2008, Andy Dbest wrote:  
> 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

I tried the puts line, just inside the method and it does return ‘1.’  
for me. It kind of annoying…

---

<div class="post-metadata">

**Author:** ![dbest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aca169/32.png) [@dbest](https://www.ruby-forum.com/u/dbest)\
**Post date:** [June 8, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/search-file-contents/139873/4 "2008-06-08T17:02:31Z")

</div>

Stefano C. wrote:

> On Sunday 08 June 2008, Andy Dbest wrote:
> 
> > > at the beginning of the method, to see whether the argument you get is  
> > > the one  
> > > you expect?
> > > 
> > > Stefano
> > 
> > I tried the puts line, just inside the method and it does return ‘1.’  
> > for me. It kind of annoying…
> 
> Can you post a sample of the file you’re searching in? This way, I can  
> try  
> your code myself and see what’s going on.
> 
> Stefano

sure…

here is an extract of the file

1. 

aaa.txt - bbb.txt  
aac.txt - bbc.tct

1. 

aac.txt - bbb.txt  
aaa.txt - bbc.txt

---

<div class="post-metadata">

**Author:** ![dbest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aca169/32.png) [@dbest](https://www.ruby-forum.com/u/dbest)\
**Post date:** [June 8, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/search-file-contents/139873/5 "2008-06-08T17:12:46Z")

</div>

On Sunday 08 June 2008, Andy Dbest wrote:

> > 
> 
> 1. 
> 
> aaa.txt - bbb.txt  
> aac.txt - bbc.tct
> 
> 1. 
> 
> aac.txt - bbb.txt  
> aaa.txt - bbc.txt

I’ve tried it and it works even without the assignment. Are you sure  
there  
isn’t any trailing space afer 1. in the method argument? Using puts as I  
suggested in my first post wouldn’t have shown that. You can try  
examining the  
argument with p, instead of puts, which displays the string with special  
characters escaped and surrounded by quotes. If everything is correct,  
the  
line

p week\_num

should output

“1.”

If there are trailing spaces, you’ll see something like

"1. "

Stefano

---

<div class="post-metadata">

**Author:** ![dbest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aca169/32.png) [@dbest](https://www.ruby-forum.com/u/dbest)\
**Post date:** [June 8, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/search-file-contents/139873/6 "2008-06-08T17:26:30Z")

</div>

Stefano C. wrote:

> On Sunday 08 June 2008, Andy Dbest wrote:

> I’ve tried it and it works even without the assignment. Are you sure  
> there  
> isn’t any trailing space afer 1. in the method argument? Using puts as I  
> suggested in my first post wouldn’t have shown that. You can try  
> examining the  
> argument with p, instead of puts, which displays the string with special  
> characters escaped and surrounded by quotes. If everything is correct,  
> the  
> line
> 
> p week\_num
> 
> should output
> 
> “1.”
> 
> If there are trailing spaces, you’ll see something like
> 
> "1. "
> 
> Stefano

Thanks Stefano,

the p week\_num line did the trick, i noticed that there was a leading  
space. I was taking the input from an FXRuby text control, in which i  
introduced a space.

Have fixed it now.

Cheers and thanks.  
Andy

---

<div class="post-metadata">

**Author:** ![dbest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aca169/32.png) [@dbest](https://www.ruby-forum.com/u/dbest)\
**Post date:** [June 8, 2008, 4:50pm UTC](https://www.ruby-forum.com/t/search-file-contents/139873/7 "2008-06-08T16:50:20Z")

</div>

On Sunday 08 June 2008, Andy Dbest wrote:

> > at the beginning of the method, to see whether the argument you get is  
> > the one  
> > you expect?
> > 
> > Stefano
> 
> I tried the puts line, just inside the method and it does return ‘1.’  
> for me. It kind of annoying…

Can you post a sample of the file you’re searching in? This way, I can  
try  
your code myself and see what’s going on.

Stefano
