Prashant
Prashant

Reputation: 13

Ruby : line.include?"#{varStrTextSearch}"

I have a file ImageContainer.xml with text as follow:

<leftArrowImage>/apps/mcaui/PAL/Arrows/C0004OptionNavArrowLeft.png</leftArrowImage>

<rightArrowImage>/apps/mcaui/PAL/Arrows/C0003OptionNavArrowRight.png</rightArrowImage>

Now, I am searching for C0004OptionNavArrowLeft.png and C0003OptionNavArrowRight.png in that file.

Code is:

@LangFileName = "ZZZPNG.txt"
fileLangInput = File.open(@LangFileName)
fileLangInput.each_line do |varStrTextSearch|

    puts "\nSearching ==>" + varStrTextSearch

    Dir.glob("**/*.*") do |file_name|
        fileSdfInput = File.open(file_name)
        fileSdfInput.each_line do |line|
            if line.include?"#{varStrTextSearch}"
                puts"Found"
            end
        end
    end
end

here varStrTextSearch is string variable having different string values.

Problem is that is it is finding C0004OptionNavArrowLeft.png but not finding C0003OptionNavArrowRight.png.

Can someone tell me where I am doing wrong?

Upvotes: 0

Views: 434

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230346

My guess is, newline chars are the problem.

fileLangInput.each_line do |varStrTextSearch|

varStrTextSearch here will contain a \n char at the end. And if your XML is not consistently formatted (for example, like this)

<leftArrowImage>
  /apps/mcaui/PAL/Arrows/C0004OptionNavArrowLeft.png
</leftArrowImage>
<rightArrowImage>/apps/mcaui/PAL/Arrows/C0003OptionNavArrowRight.png</rightArrowImage>

Then your problem can be reproduced (there's no newline char after "C0003OptionNavArrowRight", so it can't be found).

Solution? Remove the unwanted whitespace.

fileSdfInput.each_line do |line|
  if line.include? varStrTextSearch.chomp # read the docs on String#chomp
    puts"Found"
  end
end

Upvotes: 2

Related Questions