Skip to content

Instantly share code, notes, and snippets.

@iainbeeston
Created September 5, 2019 10:46
Show Gist options
  • Save iainbeeston/4da65354f98ab08650be3e87505652bb to your computer and use it in GitHub Desktop.
Save iainbeeston/4da65354f98ab08650be3e87505652bb to your computer and use it in GitHub Desktop.
Diff ruby files
#!/usr/bin/env ruby
require 'tempfile'
def get_methods(file_contents)
code_for_all_methods = file_contents.scan(/((\s+?)def .*?^\2end)/m)
code_for_all_methods.each_with_object({}) do |(code), hsh|
method_name = code[/def (?:self\.)?(\w+)/, 1]
hsh[method_name.to_s] = code
end
end
def get_methods_from_file(path)
get_methods(File.read(path))
end
def write_to_tempfile(str)
Tempfile.open do |tempfile|
tempfile.write(str)
tempfile.flush
yield tempfile.path
end
end
def delete_tempfile(tempfile)
tempfile.close
tempfile.unlink
end
def diff_methods(code_a, code_b)
write_to_tempfile(code_a) do |path_a|
write_to_tempfile(code_b) do |path_b|
return `diff --ignore-all-space #{path_a} #{path_b}`
end
end
end
first_file_path = ARGV[0]
second_file_path = ARGV[1]
first_file_methods = get_methods_from_file(first_file_path)
second_file_methods = get_methods_from_file(second_file_path)
shared_method_names = first_file_methods.keys & second_file_methods.keys
shared_method_names.each do |method_name|
puts "#{method_name}:"
difference = diff_methods(first_file_methods[method_name], second_file_methods[method_name])
puts difference.chomp == "" ? "Methods are identical" : difference
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment