Class: Repeatable::Expression::Set

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

Direct Known Subclasses

Intersection, Union

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

===, #deconstruct_keys, #difference, #include?, #intersection, #to_h, #union

Constructor Details

#initialize(elements) ⇒ Set

Returns a new instance of Set.



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

def initialize(elements)
  @elements = T.let(elements.flatten.uniq, T::Array[Expression::Base])
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



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

def elements
  @elements
end

Instance Method Details

#<<(element) ⇒ Object



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

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

#==(other) ⇒ Object



23
24
25
26
27
# File 'lib/repeatable/expression/set.rb', line 23

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