Last active
December 19, 2015 05:29
-
-
Save covard/5904585 to your computer and use it in GitHub Desktop.
How to diff two arrays in ruby and get just the values that are different between the two arrays.
This file contains 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
first_hdr = ["hello", "world"] | |
second_hdr = ["hello", "world", "!"] | |
new_columns = second_hdr - first_hdr # changed the code to use the way Morgan found, seems more ruby to me ;) | |
# output | |
# new_columns => ["!"] | |
# can also use reject! and that will reject on the second_hdr so then it would be | |
# second_hdr => ["!"] | |
# Happy Rubying =) | |
# Also see Morgan's comments below. I actully like his method better. He is a much beter developer than me and thinks of even | |
# better ways of doing things. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the first use case (the one without "reject!") you can also do:
e.g.: