Created
April 23, 2014 09:56
-
-
Save adammcarth/11209239 to your computer and use it in GitHub Desktop.
comparing 2 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comparison | |
def self.new(string_one, string_two) | |
# Define variables to manipulate later on | |
unchanged, addition = "", "" | |
all_unchanged, all_additions = [], [] | |
# Determine which file has the largest amount of characters | |
if (string_one <=> string_two) < 0 | |
num_chars = string_two.length | |
else | |
num_chars = string_one.length | |
end | |
num_chars.times do |n| | |
case | |
# WHEN the character in position n is equal in both files | |
when string_one[n] == string_two[n] | |
# ADD the character to the unchanged string | |
unchanged += string_one[n] | |
# WHEN the character in position n is different in one of the files | |
when string_one[n] != string_two[n] | |
# ADD the character to the addition string | |
addition += string_two[n] | |
# IF the next character is both files equal again, we need to place | |
# the variables into the arrays so that the files can be eventually | |
# put back together again. | |
unless string_one[n + 1] == string_two[n + 1] | |
# MERGE unchanged string into the unchanged array | |
all_unchanged << unchanged | |
# MERGE addition string into the additions array | |
all_additions << addition | |
# RESET string variables to write new content | |
unchanged, addition = "", "" | |
end | |
end | |
end | |
return "Unchanged: '#{unchanged}', Added '#{addition}'. All_Unchanged: '#{all_unchanged}', All_Added: '#{all_additions}'." | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "./comparison.rb" | |
Comparison.new("Hello", "Hello") | |
#=> Unchanged: 'Hello', Added: ''. All_Unchanged: '[]', All_Added: '[]'. | |
#=> <DESIRED OUTPUT> Unchanged: '', Added: ''. All_Unchanged: ["Hello"], All_Added: '[]'. | |
Comparison.new("Hello", "H123llo") | |
#=> "Unchanged: '', Added S: ''. All_Unchanged: '[\"H\", \"\", \"\", \"\"]', All_Added: '[\"1\", \"2\", \"3\", \"l\"]'." | |
#=> <DESIRED OUTPUT> Unchanged: '', Added: ''. All_Unchanged: ["H", "llo"], All_Added: '["123"]'. | |
Comparison.new("Welcome to GitHub", "We1lcome T0 github") | |
#=> "Unchanged: '', Added S: ''. All_Unchanged: '[\"We\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\"]', All_Added: '[\"1\", \"l\", \"c\", \"o\", \"m\", \"e\", \" \", \"T\", \"0\", \" \", \"g\", \"i\", \"t\", \"h\", \"u\"]'." | |
#=> <DESIRED OUTPUT> Unchanged: '', Added: ''. All_Unchanged: ["We", "lcome T", "it", "ub"], All_Added: '["1", "0 g", "h"]'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment