# Trying to find the number lines with a determinated word

**URL:** <https://www.ruby-forum.com/t/trying-to-find-the-number-lines-with-a-determinated-word/143272>\
**Category:** Ruby\
**Created:** [July 16, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/trying-to-find-the-number-lines-with-a-determinated-word/143272 "2008-07-16T21:58:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaizen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4af34b/32.png) [@kaizen](https://www.ruby-forum.com/u/kaizen)\
**Post date:** [July 16, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/trying-to-find-the-number-lines-with-a-determinated-word/143272/1 "2008-07-16T21:58:32Z")

</div>

Hi,

I’m trying to find the ‘line’ number where exists a predeterminated  
‘word’:

File.open(file) do |infile|  
lines = infile.readlines  
lines.each { |line| puts lines.index(line) if line.split.include? word  
}  
end

When it finds the word, it puts 0? why?

Thanks & regards,  
AndrÃ©s SuÃ¡rez

---

<div class="post-metadata">

**Author:** ![kaizen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4af34b/32.png) [@kaizen](https://www.ruby-forum.com/u/kaizen)\
**Post date:** [July 16, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/trying-to-find-the-number-lines-with-a-determinated-word/143272/2 "2008-07-16T22:13:58Z")

</div>

AndrÃ©s SuÃ¡rez wrote:

> Hi,
> 
> I’m trying to find the ‘line’ number where exists a predeterminated  
> ‘word’:
> 
> File.open(file) do |infile|  
> lines = infile.readlines  
> lines.each { |line| puts lines.index(line) if line.split.include? word  
> }  
> end
> 
> When it finds the word, it puts 0? why?
> 
> Thanks & regards,  
> AndrÃ©s SuÃ¡rez

Yepp, I know why:

-\> index returns the first match. And this is the line zero.

Regards,  
AndrÃ©s
