Compare two versions of a text file and find addit

2019-08-21 11:11发布

问题:

This question already has an answer here:

  • diff a ruby string or array 12 answers

I am tracking changes in a web-page using Ruby. After I removed all html tags and blank lines, I get an array of lines which needs to be checked for additions/removals assuming that there may be repetitions. Could you recommend a good gem if it has been done already?

I could make the array lines unique and then the problem is avoided. But what if I need to track the repeated lines as well with respect to their position in the text?

回答1:

Sounds like a textbook case of where you'd want to use the Diff algorithm.

There's a 'diff' gem, although to be fair I've never used it: http://rubygems.org/gems/diff