Class: Repeatable::Expression::Set

Inherits:
Base
  • Object
show all
Defined in:
lib/repeatable/expression/set.rb

Direct Known Subclasses

Intersection, Union

Instance Method Summary collapse

Methods inherited from Base

===, #include?

Constructor Details

#initialize(*elements) ⇒ Set

Returns a new instance of Set.



4
5
6
# File 'lib/repeatable/expression/set.rb', line 4

def initialize(*elements)
  @elements = elements.flatten.uniq
end

Instance Method Details

#<<(element) ⇒ Object



8
9
10
11
# File 'lib/repeatable/expression/set.rb', line 8

def <<(element)
  elements << element unless elements.include?(element)
  self
end

#==(other) ⇒ Object



17
18
19
20
21
# File 'lib/repeatable/expression/set.rb', line 17

def ==(other)
  other.is_a?(self.class) &&
    elements.size == other.elements.size &&
    other.elements.all? { |e| elements.include?(e) }
end

#to_hObject



13
14
15
# File 'lib/repeatable/expression/set.rb', line 13

def to_h
  Hash[hash_key, elements.map(&:to_h)]
end