Alex Rivera | Logout

diff a ruby string or array

Asked 2008-09-17T04:47:35.453
59

How do I do a diff of two strings or arrays in Ruby?

Edit
Report

1 Answer

35

For arrays, use the minus operator. For example:

>> foo = [1, 2, 3]
=> [1, 2, 3]
>> goo = [2, 3, 4]
=> [2, 3, 4]
>> foo - goo
=> [1]

Here the last line removes everything from foo that is also in goo, leaving just the element 1. I don't know how to do this for two strings, but until somebody who knows posts about it, you could just convert each string to an array, use the minus operator, and then convert the result back.

answered 2008-09-17T04:53:09.263

Your Answer