Created
September 6, 2010 23:23
-
-
Save Zapotek/567626 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# | |
# Gets the reverse diff (strings that have not changed) between 2 strings | |
# | |
# | |
# text1 = <<END | |
# This is the first test. | |
# Not really sure what else to put here... | |
# END | |
# | |
# text2 = <<END | |
# This is the second test. | |
# Not really sure what else to put here... | |
# Boo-Yah! | |
# END | |
# | |
# rdiff( text1, text2 ) # => "This is the test.\nNot really sure what else to put here...\n" | |
# | |
# | |
# @param [String] text1 | |
# @param [String] text2 | |
# | |
# @return [String] | |
# | |
def rdiff( text1, text2 ) | |
# take care of some common cases | |
return text1 if text1 == text2 | |
return '' if text1 == '' || text2 == '' | |
# get the words of the first text in an array | |
words1 = text1.split( /\b/ ) | |
# get the words of the second text in an array | |
words2 = text2.split( /\b/ ) | |
# get all the words that are different between the 2 arrays | |
# math style! | |
changes = words1 - words2 | |
changes << words2 - words1 | |
changes.flatten! | |
# get what hasn't changed (the rdiff, so to speak) as a string | |
return ( words1 - changes ).join( '' ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment