2

I have tried:

elsif file.grep(/Mode: 1/)
   puts "test"
else
   puts "test but else" 

Codacy is saying this is wrong. Is there a way to improve this? According to Codacy:

unexpected token kELSIF (Using Ruby 2.2 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)

unexpected token kELSE (Using Ruby 2.2 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)

1 Answers1

4

You can use IO::read to assign text file content and then String#include? for checking.

file_string = File.read('path/to/file')

if file_string.include?('substring')
  puts 'Yes'
else
  puts 'No'
end

Also you can replace it by String#match? (it is faster).

file_string.match?(/pattern/) will return true or false.

mechnicov
  • 12,025
  • 4
  • 33
  • 56