Last active
August 29, 2015 13:57
-
-
Save tahb/9640846 to your computer and use it in GitHub Desktop.
Array set operations
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
lunch_list = %W[beer bread bananas] | |
dinner_list = %W[beer steak chips] | |
# Combining arrays whilst preserving uniqueness | |
lunch_list | dinner_list == (lunch_list + dinner_list).uniq | |
=> ["beer", "bread", "bananas", "steak", "chips"] | |
# Adding to an array whilst preserving uniqueness | |
lunch_list |= [ "cake" ] | |
lunch_list |= [ "beer" ] | |
=> ["beer", "bread", "bananas", "cake"] | |
# Find unique elements in the first array | |
lunch_list - dinner_list | |
=> ["bread", "bananas", "cake"] | |
# Return all instance methods | |
Class.instance_methods - Object.instance_methods |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment