method
include?
include?(p1)
public
Returns true if the given object is present in self (that is, if any object == anObject), false otherwise.
a = [ "a", "b", "c" ] a.include?("b") #=> true a.include?("z") #=> false
Register or
log in
to add new notes.
mutru -
February 24, 2009
mindloaf -
May 2, 2009
Overbryd -
February 24, 2009
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"
1 thank
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]
0 thanks

