Issue #6515 has been updated by Eregon (Benoit Daloze).


lellisga (Li Ellis Galardo) wrote:
> a = [1,2,3,4,5,6,7].to_set
>  => #<Set: {1, 2, 3, 4, 5, 6, 7}> 
> b = [1,2,3].to_set
>  => #<Set: {1, 2, 3}> 
> b.subset? a
>  => true 
> 
> If you answer is yes, then we are doing this wrong because subset? or part_of method should be in a parent class (maybe Enumerable class ) in order for it to work for subset, array, hash and any data structure that inherit from it Enumerable.

Taking you example of a the first set and b the maybe-subset:
Well, probably because #subset? is O(m) (m is the size of b), and doing it naively on Array/Enumerable is O(m*n) (n being the size of a). Using (b-a).empty? is actually O(n+m) because it creates a hash (think set in this case) underneath and iterates b. Note Array#- mentions the Set library, because it's likely what you want if you need this kind of operations. Of course you might need both Array and Set operations, in which case you may:
- transform them to set when needed: which is O(n+m + m) so O(n+m).
- use (a-b).empty? which I believe is clear and concise (although a comment might help for other readers) but creates the diff Array for nothing. You might want something like:

  as = a.to_set
  b.all? { |e| as.include?(e) }

Which does not allocate an intermediary Array, and is O(n+m) as well.

Often, when you want this kind of behavior, you don't need Array-specific operations, which is why using a Set directly is better (and usually more efficient).
----------------------------------------
Feature #6515: array.c: added method that verifies if an Array is part of another
https://bugs.ruby-lang.org/issues/6515#change-27006

Author: lellisga (Li Ellis Galardo)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


This method is like the include? method but instead of receiving a value and check if the array has it, it receives an array an check if it's part of another one.

https://github.com/ruby/ruby/pull/127


-- 
http://bugs.ruby-lang.org/