Class: Draco::Set
Overview
Internal: An implementation of Set.
Instance Method Summary collapse
-
#&(other) ⇒ Object
Internal: Returns the intersection of two Sets.
-
#+(other) ⇒ Object
Internal: alias of merge.
- #==(other) ⇒ Object
-
#add(entry) ⇒ Object
Internal: Adds a new entry to the Set.
-
#delete(entry) ⇒ Object
Internal: Adds a new entry to the Set.
-
#each(&block) ⇒ Object
Internal: Returns an Enumerator for all of the entries in the Set.
-
#empty? ⇒ Boolean
Internal: Returns true if there are no entries in the Set.
-
#hash ⇒ Object
Internal: Returns a unique hash value of the Set.
-
#initialize(entries = []) ⇒ Set
constructor
Internal: Initializes a new Set.
-
#inspect ⇒ Object
Internal: Inspects the Set.
-
#member?(member) ⇒ Boolean
Internal: Returns true if the object is in the Set.
-
#merge(entries) ⇒ Object
Internal: Adds multiple objects to the Set.
-
#serialize ⇒ Object
Internal: Serializes the Set.
-
#to_a ⇒ Object
Internal: Returns an Array representation of the Set.
-
#to_s ⇒ Object
Internal: Returns a String representation of the Set.
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.
872 873 874 |
# File 'lib/draco.rb', line 872 def empty? @hash.empty? end |
#hash ⇒ Object
Internal: Returns a unique hash value of the Set.
895 896 897 |
# File 'lib/draco.rb', line 895 def hash @hash.hash end |
#inspect ⇒ Object
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.
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 |
#serialize ⇒ Object
Internal: Serializes the Set.
905 906 907 |
# File 'lib/draco.rb', line 905 def serialize to_a.inspect end |
#to_a ⇒ Object
Internal: Returns an Array representation of the Set.
900 901 902 |
# File 'lib/draco.rb', line 900 def to_a @hash.keys end |
#to_s ⇒ Object
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 |