Class: Draco::Set

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/draco.rb

Overview

Internal: An implementation of Set.

Instance Method Summary collapse

Constructor Details

#initialize(entries = []) ⇒ Set

Internal: Initializes a new Set.

entries - The initial Array list of entries for the Set



815
816
817
818
# File 'lib/draco.rb', line 815

def initialize(entries = [])
  @hash = {}
  merge(entries)
end

Instance Method Details

#&(other) ⇒ Object

Internal: Returns the intersection of two Sets.

other - The Set to intersect with

Returns a new Set of all of the common entries.



881
882
883
884
885
886
887
888
# File 'lib/draco.rb', line 881

def &(other)
  response = Set.new
  each do |key, _|
    response.add(key) if other.member?(key)
  end

  response
end

#+(other) ⇒ Object

Internal: alias of merge



851
852
853
# File 'lib/draco.rb', line 851

def +(other)
  merge(other)
end

#==(other) ⇒ Object



890
891
892
# File 'lib/draco.rb', line 890

def ==(other)
  hash == other.hash
end

#add(entry) ⇒ Object

Internal: Adds a new entry to the Set.

entry - The object to add to the Set.

Returns the Set.



825
826
827
828
# File 'lib/draco.rb', line 825

def add(entry)
  @hash[entry] = true
  self
end

#delete(entry) ⇒ Object

Internal: Adds a new entry to the Set.

entry - The object to add to the Set.

Returns the Set.



835
836
837
838
# File 'lib/draco.rb', line 835

def delete(entry)
  @hash.delete(entry)
  self
end

#each(&block) ⇒ Object

Internal: Returns an Enumerator for all of the entries in the Set.



856
857
858
# File 'lib/draco.rb', line 856

def each(&block)
  @hash.keys.each(&block)
end

#empty?Boolean

Internal: Returns true if there are no entries in the Set.

Returns a boolean.

Returns:

  • (Boolean)


872
873
874
# File 'lib/draco.rb', line 872

def empty?
  @hash.empty?
end

#hashObject

Internal: Returns a unique hash value of the Set.



895
896
897
# File 'lib/draco.rb', line 895

def hash
  @hash.hash
end

#inspectObject

Internal: Inspects the Set.



910
911
912
# File 'lib/draco.rb', line 910

def inspect
  to_a.inspect
end

#member?(member) ⇒ Boolean

Internal: Returns true if the object is in the Set.

member - The object to search the Set for.

Returns a boolean.

Returns:

  • (Boolean)


865
866
867
# File 'lib/draco.rb', line 865

def member?(member)
  @hash.key?(member)
end

#merge(entries) ⇒ Object

Internal: Adds multiple objects to the Set.

entry - The Array list of objects to add to the Set.

Returns the Set.



845
846
847
848
# File 'lib/draco.rb', line 845

def merge(entries)
  Array(entries).each { |entry| add(entry) }
  self
end

#serializeObject

Internal: Serializes the Set.



905
906
907
# File 'lib/draco.rb', line 905

def serialize
  to_a.inspect
end

#to_aObject

Internal: Returns an Array representation of the Set.



900
901
902
# File 'lib/draco.rb', line 900

def to_a
  @hash.keys
end

#to_sObject

Internal: Returns a String representation of the Set.



915
916
917
# File 'lib/draco.rb', line 915

def to_s
  to_a.to_s
end