Josh Thompson     about     blog     projects

Array divergence in Ruby

Article Table of Contents

Lets say you have a list of valid items, and you want to run another array against it, and pull out the items that don’t match.

You don’t want to iterate through all of the items in one array, calling other_array.include?(item). (That’s computationally expensive)

valid_people = ["Sarah Connor", "John Connor"]

visitor_logs = ["Sarah Connor", "John Connor", "Terminator Robot"]

You want to find any item in visitor_logs that isn’t on the approved list. How to do that?

in Ruby, you can just “subtract” one array from another:

unwanted_visitors = visitor_logs - valid_people
=> ["Terminator Robot"]

What if you want to see what items on both lists are not in common? (AKA divergence)

You’ll use Ruby’s uniq operator, which is |

Lets say you’re a really aggressive party host. When you invite people, they’d better show up. And if someone DOES show up who’s not invited, they’re in trouble too.

So, you’ve got expected_guests and actual_guests. You need to figure out who in each list isn’t on the other one. Maybe you’ll rick roll them later for their error. Here’s how you’d do that:

expected_guests = ["Sarah", "John", "The Hulk"]
actual_guests = ["Sarah", "John", "Dracula"]

expected_guests - actual_guests
=> ["The Hulk"]

actual_guests - expected_guests
=> ["Dracula"]

(expected_guests - actual_guests) | (actual_guests - expected_guests)
=> ["The Hulk", "Dracula"]

Looks like you’re rick rolling The Hulk, and Dracula. Good luck!

The above operation is the same as:

list_1 = [1, 2, 3]
list_2 = [2, 3, 4]

[1, 2, 3] | [2, 3, 4]
=> [1, 2, 3, 4]

Additional Reading #