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

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

Constructor Details

#initialize(*elements) ⇒ Set

Returns a new instance of Set.



6
7
8
# File 'lib/repeatable/expression/set.rb', line 6

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

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



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

def elements
  @elements
end

Instance Method Details

#<<(element) ⇒ Object



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

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

#==(other) ⇒ Object



19
20
21
22
23
# File 'lib/repeatable/expression/set.rb', line 19

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

#to_hObject



15
16
17
# File 'lib/repeatable/expression/set.rb', line 15

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