#!/usr/bin/env ruby # frozen_string_literal: true require "code_clone_detector" require "ripper" code = [] Dir.glob("**/*") do |file_name| if file_name.include?(".rb") File.open(file_name, "r") do |file| file_content = file.read code.push([file_name.to_s, Ripper.lex(file_content)]) end end end max = 0 sx = 0 ex = 0 sy = 0 ey = 0 index1 = 0 index2 = 0 (0..code.size - 2).each do |a| p "process has been completed.. #{a / code.size.to_f * 100}%" (a + 1..code.size - 1).each do |b| array = [] x = 0 code[a][1].each do |i| array.push([]) code[b][1].each do |j| if i[2] == j[2] array[x].push(1) else array[x].push(0) end end x += 1 end tmp_max = 0 tmp_sx = 0 tmp_ex = 0 tmp_sy = 0 tmp_ey = 0 array.size.times do |i| array[i].size.times do |j| next unless array[i - 1][j - 1].nil? || array[i - 1][j - 1].zero? next unless array[i][j] == 1 maxv = 1 x = j y = i while !array[y + 1].nil? && !array[y + 1][x + 1].nil? && array[y + 1][x + 1] == 1 maxv += 1 x += 1 y += 1 end next unless maxv > tmp_max tmp_max = maxv tmp_sx = j tmp_ex = x tmp_sy = i tmp_ey = y end end next unless tmp_max > max max = tmp_max sx = tmp_sx ex = tmp_ex sy = tmp_sy ey = tmp_ey index1 = a index2 = b end end puts "\n===================================\n\n" file_1_name = code[index1][0] file1 = code[index1][1] file_2_name = code[index2][0] file2 = code[index2][1] print "#{file_1_name}:" print "#{file1[sy][0][0]},#{file1[sy][0][1]} ~ #{file1[ey][0][0]},#{file1[ey][0][1]}\n" code[index1][1][sy..ey].transpose[2].each do |i| print i end puts "===============================\n\n" print "#{file_2_name}:" print "#{file2[sx][0][0]},#{file2[sx][0][1]} ~ #{file2[ex][0][0]},#{file2[ex][0][1]}\n" code[index2][1][sx..ex].transpose[2].each do |i| print i end