## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 6.10. Comparing Two Files

## Problem

You want to see if two files contain the same data. If they differ, you might want to represent the differences between them as a string: a patch from one to the other.

## Solution

If two files differ, it's likely that their sizes also differ, so you can often solve the problem quickly by comparing sizes. If both files are regular files with the same size, you'll need to look at their contents.

This code does the cheap checks first:

1. If one file exists and the other does not, they're not the same.

2. If neither file exists, say they're the same.

3. If the files are the same file, they're the same.

4. If the files are of different types or sizes, they're not the same.

```	class File
def File.same_contents(p1, p2)
return false if File.exists?(p1) != File.exists?(p2)
return true if !File.exists?(p1)
return true if File.expand_path(p1) == File.expand_path(p2)
return false if File.ftype(p1) != File.ftype(p2) ||
File.size(p1) != File.size(p2)```

Otherwise, it compares the files contents, a block at a time:

```	    open(p1) do |f1|
open(p2) do |f2|
blocksize = f1.lstat.blksize
same = true
while same && !f1.eof? && !f2.eof?
end
return same
end
end
end
end```

To illustrate, I'll create two identical files and compare them. I'll then make them slightly different, and compare them again.

` 1.upto(2) do |i| open("output#{i}", 'w') { |f| f << 'x' * 10000 } end File.same_contents('output1', 'output2') # => true open("output1", 'a') { |f| f << 'x' } open("output2", ...`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required