Class: SetDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/riel/setdiff.rb

Overview

Compares two enumerables, treating them as sets, showing whether they are identical, A contains B, B contains A, or A and B contain common elements.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_and_b, a_not_in_b, b_not_in_a) ⇒ SetDiff

Returns a new instance of SetDiff.



32
33
34
35
36
# File 'lib/riel/setdiff.rb', line 32

def initialize a_and_b, a_not_in_b, b_not_in_a
  @a_and_b = a_and_b
  @a_not_in_b = a_not_in_b
  @b_not_in_a = b_not_in_a
end

Instance Attribute Details

#a_and_bObject (readonly)

Returns the value of attribute a_and_b.



30
31
32
# File 'lib/riel/setdiff.rb', line 30

def a_and_b
  @a_and_b
end

#a_not_in_bObject (readonly)

Returns the value of attribute a_not_in_b.



30
31
32
# File 'lib/riel/setdiff.rb', line 30

def a_not_in_b
  @a_not_in_b
end

#b_not_in_aObject (readonly)

Returns the value of attribute b_not_in_a.



30
31
32
# File 'lib/riel/setdiff.rb', line 30

def b_not_in_a
  @b_not_in_a
end

Class Method Details

.new(a, b) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/riel/setdiff.rb', line 8

def self.new a, b
  allitems = a | b

  a_and_b = Array.new
  a_not_in_b = Array.new
  b_not_in_a = Array.new

  allitems.each do |it|
    if a.include? it
      if b.include? it
        a_and_b
      else
        a_not_in_b
      end
    else
      b_not_in_a
    end << it
  end
  
  super a_and_b, a_not_in_b, b_not_in_a
end

Instance Method Details

#diff_typeObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/riel/setdiff.rb', line 38

def diff_type
  @diff_type ||= if @a_and_b.empty?
                   :no_common
                 elsif @a_not_in_b.empty?
                   if @b_not_in_a.empty?
                     :identical
                   else
                     :b_contains_a
                   end
                 elsif @b_not_in_a.empty?
                   :a_contains_b
                 else
                   :common
                 end
end