# Opening/reading a text file - how do u use .find method?

**URL:** <https://www.ruby-forum.com/t/opening-reading-a-text-file-how-do-u-use-find-method/154641>\
**Category:** Ruby\
**Created:** [December 2, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/opening-reading-a-text-file-how-do-u-use-find-method/154641 "2008-12-02T15:45:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 2, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/opening-reading-a-text-file-how-do-u-use-find-method/154641/1 "2008-12-02T15:45:32Z")

</div>

Here I am creating a text file, writing to the text file and then  
opening the text file to search for one of the lines that I just  
inserted this line contains ‘wed’.

My issue is that when I try to search the file using the .find and a  
regular expression the file does not even enter my if statement. (This  
is very beginner level I know) What do you think or suggest that I am  
doing wrong here? MC

File.open(‘mynewfile.txt’, ‘w’)

File.open(‘mynewfile.txt’, ‘a’) do |f1|  
f1.puts “00834 tue z0sdf”  
f1.puts “01230 wed z0sdf”  
f1.puts “34234 tue wet0f”  
end

##-----\> Here is my troubled code…  
File.open(‘mynewfile.txt’, ‘r’) do |f1|  
f = File.new(“mynewfile.txt”)  
newline = f.each {|line| puts “#f.lineno}: #{line}”}  
if f.each{|line| puts “#f.lineno}: #{line}”}.find =~ /wed/  
varWed = newline  
end  
end

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 3, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/opening-reading-a-text-file-how-do-u-use-find-method/154641/2 "2008-12-03T15:16:51Z")

</div>

Not that f.each{|line| puts “#f.lineno}: #{line}”}.find returns nil, and  
nil  
=~ /wed/ returns false. Therefore, any code inside that if statement  
won’t  
get executed.

I’m not sure of what you are trying to accomplish in your code, but if  
you  
want to assign varWed the lines that contain ‘wed’, I think you should  
do  
something like this:

File.open(‘mynewfile.txt’, ‘r’) do |f|  
f.each\_line do |line|  
varWed \<\< line if line =~ /wed/  
end  
end

Or if you still want to use find method:

File.open(‘mynewfile.txt’, ‘r’) do |f|  
varWed \<\< f.find\_all { |line| line =~ /wed/ }.to\_s  
# If you only need the 1st line that contains ‘wed’, use:  
# varWed = f.find { |line| line =~ /wed/ }  
end

BTW, I didn’t know about find method before, and I was wondering from  
where  
do File objects get it in the first place. But now I know, thanks to  
your  
question 🙂

Regards,  
Yaser S.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 3, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/opening-reading-a-text-file-how-do-u-use-find-method/154641/3 "2008-12-03T15:24:55Z")

</div>

Thanks Yaser -MC
