Checking File for a line

P

Peter Loftus

Need to check a file for a line
Lets say "this is an example"

I am going to just take every line into an array
and then have a For loop to check the array for that line.

Im pretty sure this will work but is there any cleaner way to do it?
 
L

Lee Jarvis

Peter said:
Im pretty sure this will work but is there any cleaner way to do it?

File.readlines('file.txt.).each do |line|
puts 'got it!' if line =~ /regexp/
end

Hope the helps.

Regards,
Lee
 
R

Robert Klemme

2007/11/23 said:
File.readlines('file.txt.).each do |line|
puts 'got it!' if line =~ /regexp/
end

This is more efficient - no need to load the whole file into mem to
find a single line:

File.foreach "file.txt" do |line|
if /rx/ =~ line
puts "found it"
break
end
end

Cheers

robert
 
D

Dejan Dimic

This is more efficient - no need to load the whole file into mem to
find a single line:

File.foreach "file.txt" do |line|
if /rx/ =~ line
puts "found it"
break
end
end

Cheers

robert

Check out

ri IO:foreach
ri IO#each

and use what more suits your needs.

Dima
 
R

Robert Klemme

2007/11/23 said:
Check out

ri IO:foreach
ri IO#each

and use what more suits your needs.

I am not sure I understand this posting of yours. Are you talking to
the OP or to me? If you are talking to me, foreach is what I used in
the example above.

robert
 
L

Lee Jarvis

This is more efficient - no need to load the whole file into mem to
find a single line:

File.foreach "file.txt" do |line|
if /rx/ =~ line
puts "found it"
break
end
end

Cheers

robert

Ahh, good point, touche my good man.

Regards,
Lee
 
R

Robert Klemme

2007/11/23 said:
Ahh, good point, touche my good man.

While we're at it: there is another solution - probably a bit more elegant:

require 'enumerator'
File.to_enum:)foreach,"file.txt").grep(/rx/) {|li| puts li; break}

:)

Kind regards

robert
 
D

Dejan Dimic

While we're at it: there is another solution - probably a bit more elegant:

require 'enumerator'
File.to_enum:)foreach,"file.txt").grep(/rx/) {|li| puts li; break}

:)

Kind regards

robert

That's Ruby beauty, express yourself in a way that suits you the most
and still we all can understand your dialect and enjoy.

Dima
 
7

7stud --

Dejan said:
That's Ruby beauty, express yourself in a way that suits you the most
and still we all can understand your dialect and enjoy.

Speak for yourself. I found that solution particularly distasteful. It
turned a simple, easy to read solution, into a convoluted one-liner
mess. And on my system the enumerator solution is 50% slower. But,
that seems to be the Ruby Way, so at some point I guess I'll have to
drink the koolaid and declare how beautiful solutions like that are.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top