adamscott
adamscott

Reputation: 853

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

Or maybe I should stick with golf.

Upvotes: 1

Views: 47

Answers (2)

Gabor Lengyel
Gabor Lengyel

Reputation: 15599

Apparently the two files are not exact copies of each other but have different line endings. This may be a result of one file being an actual output from a linux server and the other resulting from a copy-and-paste in a Windows text editor.

Upvotes: 2

seph
seph

Reputation: 6076

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.

Upvotes: 1

Related Questions