Trying to find the number lines with a determinated word

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs