This page is READ-ONLY. It is generated from the old site.
All timestamps are relative to 2013 (when this page is generated).
If you are looking for TeX support, please go to VietTUG.org

ruby: compare two arrays

Added by over 3 years ago

I want to know whether the array A contains the array B.

Ruby doesnot support array comparision so we neeed some maths.:

B \subset A  <=>  { B \ (A \cap B) == \emptyset }

Now define the following function

class Array
  def subset?(a)
    (self - (self & a)).empty?
  end
end

and test it

  [1,2].subset? [1,2,3] => true
[1,2,3].subset? [1,2]   => false

It works :)


Comments