adamscott adamscott - 2 months ago 21
Ruby Question

Ruby array inspection of a text file converted to array using readlines

I am converting two text files to an array using readlines

f1 = File.open('current_routes.txt')
f2 = File.open('production_routes.txt')

file1lines = f1.readlines
file2lines = f2.readlines

puts file1lines.inspect
puts ''
puts file2lines.inspect


The two files are an exact copy and paste of each other:

["DEPRECATION WARNING: Sprockets method `register_engine` is deprecated.\n", "Please register a mime type using `register_mime_type` then\n"]

["DEPRECATION WARNING: Sprockets method `register_engine` is deprecated.\r\n", "Please register a mime type using `register_mime_type` then\r\n"]


Why does inspect on the first file add \n and inspection of the second file add \r\n?

I am trying to compare the files for differences and this is giving me trouble

Answer

Guessing you're on a Windows machine and the copy is adding in \r. Try this:

file1lines = f1.readlines.map(&:chomp)
file2lines = f2.readlines.map(&:chomp)

String#chomp will remove either \n or \r\n. Then you can compare the rest of the string.

Comments