forked from waypaver/ruby-diff-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparator.rb
62 lines (57 loc) · 1.52 KB
/
comparator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'set'
class Comparator
#compares two files, and prints lines of the source file that are not in the filter file
#source - filename of source file
#filter - filename of filter file
attr_accessor :source, :filter
def initialize(source_file, filter_file)
# sets class attrs.
@source = source_file
@filter = filter_file
end
def compare_files()
#compares the contents of two files, prints the unique lines from the first file
filter_set = Set.new()
File.foreach(@filter) { |line|
filter_set.add(line)
}
source_array = Array.new()
File.foreach(@source) { |line|
source_array << line
}
compare(source_array, filter_set).each{ |item|
puts " #{item}"
}
end
def compare(filtered, filter_set)
#compares an array and a set, returns array of unique items from the first array
unique_cnt = 0
unique_array = Array.new()
filtered.each { |item|
if ! filter_set.include?(item)
unique_array << item
unique_cnt += 1
end
}
puts "#{unique_cnt} unique lines found:"
unique_array
end
end
#read command line args and take appropriate action
len = ARGV.length
if len == 2
file_one_exists = File.exists?(ARGV[0])
file_two_exists = File.exists?(ARGV[1])
if !file_one_exists
puts "#{ARGV[0]} does not exist"
end
if !file_two_exists
puts "#{ARGV[1]} does not exist"
end
if file_one_exists && file_two_exists
comparator = Comparator.new(ARGV[0], ARGV[1])
comparator.compare_files()
end
else
puts "need 2 file names, #{len} provided, try this: ruby comparator.rb file1.txt file2.txt"
end