how can i compare if an array of strings contains a smaller array of strings in Ruby?
e.g.
a=["1","2","3","4","5"]
b=["2","3"]
now i want to check if a contains b and get true/false
Thanks.
The most common approach would be to
(b - a).empty?
It has issues with unique elements, though. To detect whether a includes all elements from b, one should:
a_copy = a.dup
b.all? { |e| a_copy.delete e }
# or
b.all?(&a_copy.method(:delete))
b.all? { |x| a.include(x) }
(a & b) == b, it will return boolean valuetrue/false&squeezes duplicates resulting in weird[1, 1] & [1, 1] == [1, 1] #⇒ falsebecause surprisingly[1, 1] & [1, 1] == [1].