Class: Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/aio/base/toolkit/diff.rb

Overview

module Aio::Base::Toolkit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_1, input_2) ⇒ Diff

Returns a new instance of Diff.



12
13
14
15
16
# File 'lib/aio/base/toolkit/diff.rb', line 12

def initialize(input_1, input_2)
	self.input_1 = File.open(input_1)
	self.input_2 = File.open(input_2)
	@context_name = "context"
end

Instance Attribute Details

#input_1Object

输入文件1



7
8
9
# File 'lib/aio/base/toolkit/diff.rb', line 7

def input_1
  @input_1
end

#input_2Object

输入文件2



10
11
12
# File 'lib/aio/base/toolkit/diff.rb', line 10

def input_2
  @input_2
end

Instance Method Details

#context_name=(var) ⇒ Object



88
89
90
# File 'lib/aio/base/toolkit/diff.rb', line 88

def context_name=(var)
	@context_name = var
end

#diffObject



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aio/base/toolkit/diff.rb', line 18

def diff
	in_1 = @input_1.dup
	in_2 = @input_2.dup.readlines
	# 行计数从1开始
	line_count = 0
	res_text = []

	in_1.each_line do |line_1|
		# 加入了区间,如果不是在这个区间内,则忽略
		line_count += 1
		unless @range.nil?
			if ! @range.include?(line_count)
				next
			end
		end

		line_1 = line_1.chomp
		begin
			line_2 = in_2[line_count - 1].chomp
		rescue EOFError
			break
		end
		#new_line = "#{@context_name}[#{line_count}].match_block(/"
		new_line = "#{@context_name}.readline_match_block(/"
		
		# 按照单词进行比对
		word_1_arr = line_1.split(' ')
		word_2_arr = line_2.split(' ')

		word_arr = []
		merge_count = []
		word_1_arr.each_with_index do |word_1, i|

			word_2 = word_2_arr[i]
			
			if word_1 == word_2
				word_arr << inn(word_1)
			else
				word_arr << '(.*)'
				merge_count << i
			end
		end
		# 合并word_arr中相邻的(.*)
		real_word_arr = []
		word_arr.each_with_index do |word, i|
			if word != '(.*)'
				real_word_arr << word
				next
			end
			if word_arr[i+1] == '(.*)'
				next
			end
				# 加入(.*)
				real_word_arr << word
		end

		new_line << real_word_arr.join(' ')
		new_line << '/)'

		res_text << new_line
	end

	res = res_text.join("\n")
	puts res
end

#inn(word) ⇒ Object

无害化处理



93
94
95
96
97
98
99
# File 'lib/aio/base/toolkit/diff.rb', line 93

def inn(word)
	word.gsub!('/', '\/')
	word.gsub!('(', '\(')
	word.gsub!(')', '\)')
	word.gsub!('.', '\.')
	word
end

#set_range(first, last) ⇒ Object



84
85
86
# File 'lib/aio/base/toolkit/diff.rb', line 84

def set_range(first, last)
	@range = Range.new(first, last)
end