I want to find the easiest way if one array is a subset of another
for example
A b
[1,2,4,5,6,7,9] [1,3,9] =>true
A b
[1,2,4,5,6,7,9] [1,3,8] =>false
I want to find the easiest way if one array is a subset of another
for example
A b
[1,2,4,5,6,7,9] [1,3,9] =>true
A b
[1,2,4,5,6,7,9] [1,3,8] =>false
Oop, i already found
b.to_set.subset?(a.to_set)
A b
[1,2,4,5,6,7,9] [1,3,9] =>trueA b
[1,2,4,5,6,7,9] [1,3,8] =>falsePosted via http://www.ruby-forum.com/.
If I understand correctly what you really want, the first example should
produce false, as array “a” does not have 3. Assuming that you meant
[1,2,3,4,5,6,7,9] I think the following will work for you:
(a & b) == b
Gennady.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs