0

I'm going through a file line by line and looking for the word "BIOS" in each line because each line that contains the word BIOS has a version number I need. After I see that the line contains the word "BIOS" I want to take the entire line, and split it into an array. Here's my code:

File.open(file).each do |line|
   if line.includes? 'BIOS'
     array = line.split(" ")
   end
end

The problem I'm having is that I keep getting an error, saying that "includes?" is an undefined method. Is there a better way to parse each line of this file for a specific string?

pjano1
  • 143
  • 1
  • 3
  • 10
  • 2
    Don't you mean [`include?`](http://ruby-doc.org/core-1.9.3/String.html#method-i-include-3F), not plural `includes?` – deefour Oct 02 '18 at 15:17
  • I know what you mean by, "I keep getting an error...": it looks OK, so maybe if I run it again it will work. It makes no sense but I expect we've all done that. You need to take Ruby at her word. If she says a method doesn't exist, it doesn't. Here you should have looked for `includes?` in the documentation for [String](http://ruby-doc.org/core-2.5.1/String.html). You'd then have spotted your mistake. Alas, I don't think others are likely to learn anything from this question so you may wish to consider deleting it. – Cary Swoveland Oct 02 '18 at 17:24
  • Consider using [IO::foreach](http://ruby-doc.org/core-2.5.1/IO.html#method-c-foreach): `File.foreach(file) do |line| ...`. btw, `File.superclass #=> IO`, which is why you often see `IO` methods having `File` as their receiver. – Cary Swoveland Oct 02 '18 at 17:33

1 Answers1

2

That's right, includes? is not defined for String in Ruby. Use include? instead, or []: https://ruby-doc.org/core-2.2.0/String.html#method-i-include-3F

So the code should be:

File.open(file).each do |line|
   if line.include? 'BIOS'
     array = line.split(' ')
   end
end
Ilya Konyukhov
  • 2,666
  • 1
  • 12
  • 21