Module: Set::SubclassCompatible
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
| Defined in: | lib/set/subclass_compatible.rb |
Overview
This module is automatically included in subclasses of ::Set, to make them backwards compatible with the pure-Ruby set implementation used before Ruby 4. Users who want to use ::Set subclasses without this compatibility layer should subclass from Set::CoreSet.
Note that ::Set subclasses that access @hash are not compatible even with this support. Such subclasses must be updated to support Ruby 4.
Constant Summary
-
InspectKey =
Internal use only
# File 'lib/set/subclass_compatible.rb', line 316:__inspect_key__
Instance Method Summary
- #&(enum) (also: #intersection)
-
#+(enum)
Alias for #|.
- #-(enum) (also: #difference)
-
#<(set)
Alias for #proper_subset?.
-
#<=(set)
Alias for #subset?.
- #<=>(set)
- #==(other)
-
#>(set)
Alias for #proper_superset?.
-
#>=(set)
Alias for #superset?.
- #^(enum)
- #add?(o) ⇒ Boolean
- #classify
- #collect! (also: #map!)
- #delete?(o) ⇒ Boolean
- #delete_if(&block)
-
#difference(enum)
Alias for #-.
- #disjoint?(set) ⇒ Boolean
-
#filter!(&block)
Alias for #select!.
- #flatten
- #flatten!
-
#initialize(enum = nil, &block)
Creates a new set containing the elements of the given enumerable object.
-
#inspect
(also: #to_s)
Returns a string containing a human-readable representation of the set (“#<Set:
element2, …>”). - #intersect?(set) ⇒ Boolean
-
#intersection(enum)
Alias for #&.
- #join(separator = nil)
- #keep_if(&block)
-
#map!
Alias for #collect!.
- #merge(*enums)
- #proper_subset?(set) ⇒ Boolean (also: #<)
- #proper_superset?(set) ⇒ Boolean (also: #>)
- #reject!(&block)
- #replace(enum)
- #select!(&block) (also: #filter!)
- #subset?(set) ⇒ Boolean (also: #<=)
- #subtract(enum)
- #superset?(set) ⇒ Boolean (also: #>=)
-
#to_s
Alias for #inspect.
- #to_set(*args, &block)
-
#union(enum)
Alias for #|.
- #|(enum) (also: #+, #union)
- #eql?(o) ⇒ Boolean Internal use only
- #pretty_print(pp) Internal use only
- #pretty_print_cycle(pp) Internal use only
- #flatten_merge(set, seen = {}) protected Internal use only
- #do_with_enum(enum, &block) private Internal use only
Instance Method Details
#&(enum) Also known as: #intersection
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 262
def &(enum) n = self.class.new if enum.is_a?(Set) if enum.size > size each { |o| n.add(o) if enum.include?(o) } else enum.each { |o| n.add(o) if include?(o) } end else do_with_enum(enum) { |o| n.add(o) if include?(o) } end n end
#+(enum)
Alias for #|.
# File 'lib/set/subclass_compatible.rb', line 254
alias + |
#-(enum) Also known as: #difference
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 257
def -(enum) dup.subtract(enum) end
#<(set)
Alias for #proper_subset?.
# File 'lib/set/subclass_compatible.rb', line 159
alias < proper_subset?
#<=(set)
Alias for #subset?.
# File 'lib/set/subclass_compatible.rb', line 147
alias <= subset?
#<=>(set)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 161
def <=>(set) return unless set.is_a?(Set) case size <=> set.size when -1 then -1 if proper_subset?(set) when +1 then +1 if proper_superset?(set) else 0 if self.==(set) end end
#==(other)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 283
def ==(other) if self.equal?(other) true elsif other.instance_of?(self.class) super elsif other.is_a?(Set) && self.size == other.size other.all? { |o| include?(o) } else false end end
#>(set)
Alias for #proper_superset?.
# File 'lib/set/subclass_compatible.rb', line 135
alias > proper_superset?
#>=(set)
Alias for #superset?.
# File 'lib/set/subclass_compatible.rb', line 123
alias >= superset?
#^(enum)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 277
def ^(enum) n = self.class.new(enum) each { |o| n.add(o) unless n.delete?(o) } n end
#add?(o) ⇒ Boolean
# File 'lib/set/subclass_compatible.rb', line 190
def add?(o) add(o) unless include?(o) end
#classify
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 300
def classify block_given? or return enum_for(__method__) { size } h = {} each { |i| (h[yield(i)] ||= self.class.new).add(i) } h end
#collect! Also known as: #map!
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 210
def collect! block_given? or return enum_for(__method__) { size } set = self.class.new each { |o| set << yield(o) } replace(set) end
#delete?(o) ⇒ Boolean
# File 'lib/set/subclass_compatible.rb', line 194
def delete?(o) delete(o) if include?(o) end
#delete_if(&block)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 198
def delete_if(&block) block_given? or return enum_for(__method__) { size } select(&block).each { |o| delete(o) } self end
#difference(enum)
Alias for #-.
# File 'lib/set/subclass_compatible.rb', line 260
alias difference -
#disjoint?(set) ⇒ Boolean
# File 'lib/set/subclass_compatible.rb', line 186
def disjoint?(set) !intersect?(set) end
#do_with_enum(enum, &block) (private)
# File 'lib/set/subclass_compatible.rb', line 51
def do_with_enum(enum, &block) # :nodoc: if enum.respond_to?(:each_entry) enum.each_entry(&block) if block elsif enum.respond_to?(:each) enum.each(&block) if block else raise ArgumentError, "value must be enumerable" end end
#eql?(o) ⇒ Boolean
# File 'lib/set/subclass_compatible.rb', line 295
def eql?(o) # :nodoc: return false unless o.is_a?(Set) super end
#filter!(&block)
Alias for #select!.
# File 'lib/set/subclass_compatible.rb', line 232
alias filter! select!
#flatten
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 105
def flatten self.class.new.flatten_merge(self) end
#flatten!
[ GitHub ]#flatten_merge(set, seen = {}) (protected)
# File 'lib/set/subclass_compatible.rb', line 83
def flatten_merge(set, seen = {}) # :nodoc: set.each { |e| if e.is_a?(Set) case seen[e_id = e.object_id] when true raise ArgumentError, "tried to flatten recursive Set" when false next end seen[e_id] = true flatten_merge(e, seen) seen[e_id] = false else add(e) end } self end
#initialize(enum = nil, &block)
Creates a new set containing the elements of the given enumerable object.
If a block is given, the elements of enum are preprocessed by the given block.
Set.new([1, 2]) #=> #<Set: {1, 2}>
Set.new([1, 2, 1]) #=> #<Set: {1, 2}>
Set.new([1, 'c', :s]) #=> #<Set: {1, "c", :s}>
Set.new(1..5) #=> #<Set: {1, 2, 3, 4, 5}>
Set.new([1, 2, 3]) { |x| x * x } #=> #<Set: {1, 4, 9}>
# File 'lib/set/subclass_compatible.rb', line 41
def initialize(enum = nil, &block) # :yields: o enum.nil? and return if block do_with_enum(enum) { |o| add(block[o]) } else merge(enum) end end
#inspect Also known as: #to_s
Returns a string containing a human-readable representation of the set (“#<Set: element2, …>”).
# File 'lib/set/subclass_compatible.rb', line 320
def inspect ids = (Thread.current[InspectKey] ||= []) if ids.include?(object_id) return sprintf('#<%s: {...}>', self.class.name) end ids << object_id begin return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2]) ensure ids.pop end end
#intersect?(set) ⇒ Boolean
# File 'lib/set/subclass_compatible.rb', line 171
def intersect?(set) case set when Set if size < set.size any?(set) else set.any?(self) end when Enumerable set.any?(self) else raise ArgumentError, "value must be enumerable" end end
#intersection(enum)
Alias for #&.
# File 'lib/set/subclass_compatible.rb', line 275
alias intersection &
#join(separator = nil)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 312
def join(separator=nil) to_a.join(separator) end
#keep_if(&block)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 204
def keep_if(&block) block_given? or return enum_for(__method__) { size } reject(&block).each { |o| delete(o) } self end
#map!
Alias for #collect!.
# File 'lib/set/subclass_compatible.rb', line 216
alias map! collect!
#merge(*enums)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 234
def merge(*enums, **nil) enums.each do |enum| if enum.instance_of?(self.class) super(enum) else do_with_enum(enum) { |o| add(o) } end end self end
#pretty_print(pp)
# File 'lib/set/subclass_compatible.rb', line 337
def pretty_print(pp) # :nodoc: pp.group(1, sprintf('#<%s:', self.class.name), '>') { pp.breakable pp.group(1, '{', '}') { pp.seplist(self) { |o| pp.pp o } } } end
#pretty_print_cycle(pp)
# File 'lib/set/subclass_compatible.rb', line 348
def pretty_print_cycle(pp) # :nodoc: pp.text sprintf('#<%s: {%s}>', self.class.name, empty? ? '' : '...') end
#proper_subset?(set) ⇒ Boolean
Also known as: #<
# File 'lib/set/subclass_compatible.rb', line 149
def proper_subset?(set) case when set.instance_of?(self.class) super when set.is_a?(Set) size < set.size && all?(set) else raise ArgumentError, "value must be a set" end end
#proper_superset?(set) ⇒ Boolean
Also known as: #>
# File 'lib/set/subclass_compatible.rb', line 125
def proper_superset?(set) case when set.instance_of?(self.class) super when set.is_a?(Set) size > set.size && set.all?(self) else raise ArgumentError, "value must be a set" end end
#reject!(&block)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 218
def reject!(&block) block_given? or return enum_for(__method__) { size } n = size delete_if(&block) self if size != n end
#replace(enum)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 62
def replace(enum) if enum.instance_of?(self.class) super else do_with_enum(enum) # make sure enum is enumerable before calling clear clear merge(enum) end end
#select!(&block) Also known as: #filter!
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 225
def select!(&block) block_given? or return enum_for(__method__) { size } n = size keep_if(&block) self if size != n end
#subset?(set) ⇒ Boolean
Also known as: #<=
# File 'lib/set/subclass_compatible.rb', line 137
def subset?(set) case when set.instance_of?(self.class) super when set.is_a?(Set) size <= set.size && all?(set) else raise ArgumentError, "value must be a set" end end
#subtract(enum)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 246
def subtract(enum) do_with_enum(enum) { |o| delete(o) } self end
#superset?(set) ⇒ Boolean
Also known as: #>=
# File 'lib/set/subclass_compatible.rb', line 113
def superset?(set) case when set.instance_of?(self.class) super when set.is_a?(Set) size >= set.size && set.all?(self) else raise ArgumentError, "value must be a set" end end
#to_s
Alias for #inspect.
# File 'lib/set/subclass_compatible.rb', line 335
alias to_s inspect
#to_set(*args, &block)
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 72
def to_set(*args, &block) klass = if args.empty? Set else warn "passing arguments to Enumerable#to_set is deprecated", uplevel: 1 args.shift end return self if instance_of?(Set) && klass == Set && block.nil? && args.empty? klass.new(self, *args, &block) end
#union(enum)
Alias for #|.
# File 'lib/set/subclass_compatible.rb', line 255
alias union |
#|(enum) Also known as: #+, #union
[ GitHub ]# File 'lib/set/subclass_compatible.rb', line 251
def |(enum) dup.merge(enum) end