Trying to find the number lines with a determinated word

  • Thread starter Andrés Suárez
  • Start date
A

Andrés Suárez

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
 
A

Andrés Suárez

Andrés Suárez said:
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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top