method
include?
![Extensive documentation Importance_4](https://d2vfyqvduarcvs.cloudfront.net/images/importance_4.png?1349367920)
include?(p1)
public
Register or
log in
to add new notes.
mindloaf -
May 2, 2009
anoiaque -
June 22, 2012 - (v1_8_6_287 - v1_9_3_125)
mutru -
February 24, 2009
Overbryd -
February 24, 2009
colmac -
November 15, 2013
![Default_avatar_30](https://www.gravatar.com/avatar/405f82a534decdf47041a5c8dc05ba3f?default=http://apidock.com/images/default_avatar_30.png&size=30)
4 thanks
Test if one array includes the elements of another
You can just use a set difference (aka minus) to see if one array includes all elements of another
not_included = [1,2,3] - (1..9).to_a not_included # => [] not_included = [1,2,3,'A'] - (1..9).to_a not_included # => ["A"]
Use intersection to test if any of the one are in the other:
shared = [1,2,3,'A'] & (1..9).to_a shared # => [1, 2, 3]
![Default_avatar_30](https://www.gravatar.com/avatar/4f399ff54aa7e90535716f6c5120897a?default=http://apidock.com/images/default_avatar_30.png&size=30)
3 thanks
Test if an array is included in another
Array
class Array def included_in? array array.to_set.superset?(self.to_set) end end [1,2,4].included_in?([1,10,2,34,4]) #=> true
![Default_avatar_30](https://www.gravatar.com/avatar/40e75e91ba81120383c7223690e9bcc0?default=http://apidock.com/images/default_avatar_30.png&size=30)
2 thanks
Test if one array includes the elements of another v2
Maybe a bit more readable way to write the previous snippet would’ve been
puts "yay" if [1, 2, 3].all? { |i| (1..9).include?(i) } # => "yay" puts "nope" if [1, 2, 3, 'A'].any? { |i| not (1..9).include?(i) } # => "nope"
![Default_avatar_30](https://www.gravatar.com/avatar/171de85d4d79cb7afe725174fb099137?default=http://apidock.com/images/default_avatar_30.png&size=30)
0 thanks
![Default_avatar_30](https://www.gravatar.com/avatar/8f04cef0d283ae7fd67eb8a1cc641d7a?default=http://apidock.com/images/default_avatar_30.png&size=30)
0 thanks
Test if an array is included in another
a note for anoiaque solution…
before running you need to require set
require 'set' class Array def included_in? array array.to_set.superset(self.to_set) end end [1,2,4].included_in?([1,10,2,34,4]) #=> true