Skip to content

Instantly share code, notes, and snippets.

@tahb
Last active August 29, 2015 13:57
Show Gist options
  • Save tahb/9640846 to your computer and use it in GitHub Desktop.
Save tahb/9640846 to your computer and use it in GitHub Desktop.
Array set operations
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