I would like to know the difference between two strings and I’m using diff-lcs to do this. However, I am having trouble parsing the output.
require 'diff-lcs'
a = 'abcdef'
b = 'abcsef'
diffs = Diff::LCS.diff(a,b)
puts diffs.inspect
==> [[#<Diff::LCS::Change:0x0000010106dcc0 @action="-", @position=3, @element="d">, #<Diff::LCS::Change:0x0000010106db30 @action="+", @position=3, @element="s">], [#<Diff::LCS::Change:0x0000010106d360 @action="-", @position=6, @element="">]]
I would like to output the character differences only, so ‘d’ and ‘s’. Is there a way to extract the @element from that output?
Thanks
The character(element) is a read-only attribute that you can access.
The generated output is an array of arrays.Each second array contains one change. The second array is of size 2 and includes the change in each string.
I don’t know how general you want this to be but for this case the following will work:
If you have many matches perhaps a loop of some sort :