Skip to content

Instantly share code, notes, and snippets.

@softprops
Created February 14, 2009 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softprops/64251 to your computer and use it in GitHub Desktop.
Save softprops/64251 to your computer and use it in GitHub Desktop.
# how to I tell if an array includes all elements of another array?
[1,2,3].include? [1,2,3] => false # expects single element
[[1,2,3]].include? [1,2,3] => true # works if ordered and includes all
[[1,3,2]].include? [1,2,3] => false # fails if unordered
[[1,3,2].sort].include? [1,2,3].sort => true # passes if ordered
[[1,2,3]].include? [1,2] => false => # fails because it expects the full array
# fallback on uniq! ?
items = [1,2,3,4]
[5,6,7].each { |a| items << a }
[5,6,7].each { |a| items << a }
items # => [1, 2, 3, 4, 5, 6, 7, 5, 6, 7]
items.uniq! # => [1, 2, 3, 4, 5, 6, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment