include? only true once

  • Thread starter Matthew Margolis
  • Start date
M

Matthew Margolis

blockFile = File.new("blocklist.txt", "a+")
print blockFile.read.include?("aa")
print blockFile.read.include?("aa")

blocklist.txt looks like
--------------------------------------------
aa
ae
aj
au
ae
---------------------------------------------


Why does #include? return true on the first call only?


Thank you,
Matthew Margolis
 
D

David A. Black

Hi --

blockFile = File.new("blocklist.txt", "a+")
print blockFile.read.include?("aa")
print blockFile.read.include?("aa")

blocklist.txt looks like
--------------------------------------------
aa
ae
aj
au
ae

Because you've reached end-of-file :)


David
 
M

Matthew Margolis

David said:
Hi --



Because you've reached end-of-file :)


David
blockFile = File.new("blocklist.txt", "a+")
print blockFile.read.include?("aa")
blockFile.lineno = 0
print blockFile.read.include?("aa")

Still gives true false instead of true true

-Matthew Margolis
 
D

David A. Black

Hi --

blockFile = File.new("blocklist.txt", "a+")
print blockFile.read.include?("aa")
blockFile.lineno = 0
print blockFile.read.include?("aa")

Still gives true false instead of true true

I'm ignorant of #lineno= but it doesn't seem to affect EOF. (If you
add an EOF test it will come out true.) But you could use #pos to get
back to the beginning of the stream.


David
 
M

Matthew Margolis

David said:
Hi --



I'm ignorant of #lineno= but it doesn't seem to affect EOF. (If you
add an EOF test it will come out true.) But you could use #pos to get
back to the beginning of the stream.


David
Excellent, thank you.

-Matthew Margolis
 
R

Robert Klemme

Matthew Margolis said:
Excellent, thank you.

-Matthew Margolis

Why don't you just:

# complete:
content = File.read("blocklist.txt")
print content.include?("aa")
print content.include?("aa")

# line oriented:
content = File.readlines("blocklist.txt")
print content.include?("aa")
print content.include?("aa")


Much more efficient if the file fits into mem.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top