Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby30/share/ruby
File: set.rb
# frozen_string_literal: true
[0] Fix | Delete
# :markup: markdown
[1] Fix | Delete
#
[2] Fix | Delete
# set.rb - defines the Set class
[3] Fix | Delete
#
[4] Fix | Delete
# Copyright (c) 2002-2020 Akinori MUSHA <knu@iDaemons.org>
[5] Fix | Delete
#
[6] Fix | Delete
# Documentation by Akinori MUSHA and Gavin Sinclair.
[7] Fix | Delete
#
[8] Fix | Delete
# All rights reserved. You can redistribute and/or modify it under the same
[9] Fix | Delete
# terms as Ruby.
[10] Fix | Delete
[11] Fix | Delete
[12] Fix | Delete
##
[13] Fix | Delete
# This library provides the Set class, which deals with a collection
[14] Fix | Delete
# of unordered values with no duplicates. It is a hybrid of Array's
[15] Fix | Delete
# intuitive inter-operation facilities and Hash's fast lookup.
[16] Fix | Delete
#
[17] Fix | Delete
# The method `to_set` is added to Enumerable for convenience.
[18] Fix | Delete
#
[19] Fix | Delete
# Set implements a collection of unordered values with no duplicates.
[20] Fix | Delete
# This is a hybrid of Array's intuitive inter-operation facilities and
[21] Fix | Delete
# Hash's fast lookup.
[22] Fix | Delete
#
[23] Fix | Delete
# Set is easy to use with Enumerable objects (implementing `each`).
[24] Fix | Delete
# Most of the initializer methods and binary operators accept generic
[25] Fix | Delete
# Enumerable objects besides sets and arrays. An Enumerable object
[26] Fix | Delete
# can be converted to Set using the `to_set` method.
[27] Fix | Delete
#
[28] Fix | Delete
# Set uses Hash as storage, so you must note the following points:
[29] Fix | Delete
#
[30] Fix | Delete
# * Equality of elements is determined according to Object#eql? and
[31] Fix | Delete
# Object#hash. Use Set#compare_by_identity to make a set compare
[32] Fix | Delete
# its elements by their identity.
[33] Fix | Delete
# * Set assumes that the identity of each element does not change
[34] Fix | Delete
# while it is stored. Modifying an element of a set will render the
[35] Fix | Delete
# set to an unreliable state.
[36] Fix | Delete
# * When a string is to be stored, a frozen copy of the string is
[37] Fix | Delete
# stored instead unless the original string is already frozen.
[38] Fix | Delete
#
[39] Fix | Delete
# ## Comparison
[40] Fix | Delete
#
[41] Fix | Delete
# The comparison operators `<`, `>`, `<=`, and `>=` are implemented as
[42] Fix | Delete
# shorthand for the {proper_,}{subset?,superset?} methods. The `<=>`
[43] Fix | Delete
# operator reflects this order, or return `nil` for sets that both
[44] Fix | Delete
# have distinct elements (`{x, y}` vs. `{x, z}` for example).
[45] Fix | Delete
#
[46] Fix | Delete
# ## Example
[47] Fix | Delete
#
[48] Fix | Delete
# ```ruby
[49] Fix | Delete
# require 'set'
[50] Fix | Delete
# s1 = Set[1, 2] #=> #<Set: {1, 2}>
[51] Fix | Delete
# s2 = [1, 2].to_set #=> #<Set: {1, 2}>
[52] Fix | Delete
# s1 == s2 #=> true
[53] Fix | Delete
# s1.add("foo") #=> #<Set: {1, 2, "foo"}>
[54] Fix | Delete
# s1.merge([2, 6]) #=> #<Set: {1, 2, "foo", 6}>
[55] Fix | Delete
# s1.subset?(s2) #=> false
[56] Fix | Delete
# s2.subset?(s1) #=> true
[57] Fix | Delete
# ```
[58] Fix | Delete
#
[59] Fix | Delete
# ## Contact
[60] Fix | Delete
#
[61] Fix | Delete
# - Akinori MUSHA <<knu@iDaemons.org>> (current maintainer)
[62] Fix | Delete
#
[63] Fix | Delete
class Set
[64] Fix | Delete
include Enumerable
[65] Fix | Delete
[66] Fix | Delete
# Creates a new set containing the given objects.
[67] Fix | Delete
#
[68] Fix | Delete
# Set[1, 2] # => #<Set: {1, 2}>
[69] Fix | Delete
# Set[1, 2, 1] # => #<Set: {1, 2}>
[70] Fix | Delete
# Set[1, 'c', :s] # => #<Set: {1, "c", :s}>
[71] Fix | Delete
def self.[](*ary)
[72] Fix | Delete
new(ary)
[73] Fix | Delete
end
[74] Fix | Delete
[75] Fix | Delete
# Creates a new set containing the elements of the given enumerable
[76] Fix | Delete
# object.
[77] Fix | Delete
#
[78] Fix | Delete
# If a block is given, the elements of enum are preprocessed by the
[79] Fix | Delete
# given block.
[80] Fix | Delete
#
[81] Fix | Delete
# Set.new([1, 2]) #=> #<Set: {1, 2}>
[82] Fix | Delete
# Set.new([1, 2, 1]) #=> #<Set: {1, 2}>
[83] Fix | Delete
# Set.new([1, 'c', :s]) #=> #<Set: {1, "c", :s}>
[84] Fix | Delete
# Set.new(1..5) #=> #<Set: {1, 2, 3, 4, 5}>
[85] Fix | Delete
# Set.new([1, 2, 3]) { |x| x * x } #=> #<Set: {1, 4, 9}>
[86] Fix | Delete
def initialize(enum = nil, &block) # :yields: o
[87] Fix | Delete
@hash ||= Hash.new(false)
[88] Fix | Delete
[89] Fix | Delete
enum.nil? and return
[90] Fix | Delete
[91] Fix | Delete
if block
[92] Fix | Delete
do_with_enum(enum) { |o| add(block[o]) }
[93] Fix | Delete
else
[94] Fix | Delete
merge(enum)
[95] Fix | Delete
end
[96] Fix | Delete
end
[97] Fix | Delete
[98] Fix | Delete
# Makes the set compare its elements by their identity and returns
[99] Fix | Delete
# self. This method may not be supported by all subclasses of Set.
[100] Fix | Delete
def compare_by_identity
[101] Fix | Delete
if @hash.respond_to?(:compare_by_identity)
[102] Fix | Delete
@hash.compare_by_identity
[103] Fix | Delete
self
[104] Fix | Delete
else
[105] Fix | Delete
raise NotImplementedError, "#{self.class.name}\##{__method__} is not implemented"
[106] Fix | Delete
end
[107] Fix | Delete
end
[108] Fix | Delete
[109] Fix | Delete
# Returns true if the set will compare its elements by their
[110] Fix | Delete
# identity. Also see Set#compare_by_identity.
[111] Fix | Delete
def compare_by_identity?
[112] Fix | Delete
@hash.respond_to?(:compare_by_identity?) && @hash.compare_by_identity?
[113] Fix | Delete
end
[114] Fix | Delete
[115] Fix | Delete
def do_with_enum(enum, &block) # :nodoc:
[116] Fix | Delete
if enum.respond_to?(:each_entry)
[117] Fix | Delete
enum.each_entry(&block) if block
[118] Fix | Delete
elsif enum.respond_to?(:each)
[119] Fix | Delete
enum.each(&block) if block
[120] Fix | Delete
else
[121] Fix | Delete
raise ArgumentError, "value must be enumerable"
[122] Fix | Delete
end
[123] Fix | Delete
end
[124] Fix | Delete
private :do_with_enum
[125] Fix | Delete
[126] Fix | Delete
# Dup internal hash.
[127] Fix | Delete
def initialize_dup(orig)
[128] Fix | Delete
super
[129] Fix | Delete
@hash = orig.instance_variable_get(:@hash).dup
[130] Fix | Delete
end
[131] Fix | Delete
[132] Fix | Delete
if Kernel.instance_method(:initialize_clone).arity != 1
[133] Fix | Delete
# Clone internal hash.
[134] Fix | Delete
def initialize_clone(orig, **options)
[135] Fix | Delete
super
[136] Fix | Delete
@hash = orig.instance_variable_get(:@hash).clone(**options)
[137] Fix | Delete
end
[138] Fix | Delete
else
[139] Fix | Delete
# Clone internal hash.
[140] Fix | Delete
def initialize_clone(orig)
[141] Fix | Delete
super
[142] Fix | Delete
@hash = orig.instance_variable_get(:@hash).clone
[143] Fix | Delete
end
[144] Fix | Delete
end
[145] Fix | Delete
[146] Fix | Delete
def freeze # :nodoc:
[147] Fix | Delete
@hash.freeze
[148] Fix | Delete
super
[149] Fix | Delete
end
[150] Fix | Delete
[151] Fix | Delete
# Returns the number of elements.
[152] Fix | Delete
def size
[153] Fix | Delete
@hash.size
[154] Fix | Delete
end
[155] Fix | Delete
alias length size
[156] Fix | Delete
[157] Fix | Delete
# Returns true if the set contains no elements.
[158] Fix | Delete
def empty?
[159] Fix | Delete
@hash.empty?
[160] Fix | Delete
end
[161] Fix | Delete
[162] Fix | Delete
# Removes all elements and returns self.
[163] Fix | Delete
#
[164] Fix | Delete
# set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}>
[165] Fix | Delete
# set.clear #=> #<Set: {}>
[166] Fix | Delete
# set #=> #<Set: {}>
[167] Fix | Delete
def clear
[168] Fix | Delete
@hash.clear
[169] Fix | Delete
self
[170] Fix | Delete
end
[171] Fix | Delete
[172] Fix | Delete
# Replaces the contents of the set with the contents of the given
[173] Fix | Delete
# enumerable object and returns self.
[174] Fix | Delete
#
[175] Fix | Delete
# set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}>
[176] Fix | Delete
# set.replace([1, 2]) #=> #<Set: {1, 2}>
[177] Fix | Delete
# set #=> #<Set: {1, 2}>
[178] Fix | Delete
def replace(enum)
[179] Fix | Delete
if enum.instance_of?(self.class)
[180] Fix | Delete
@hash.replace(enum.instance_variable_get(:@hash))
[181] Fix | Delete
self
[182] Fix | Delete
else
[183] Fix | Delete
do_with_enum(enum) # make sure enum is enumerable before calling clear
[184] Fix | Delete
clear
[185] Fix | Delete
merge(enum)
[186] Fix | Delete
end
[187] Fix | Delete
end
[188] Fix | Delete
[189] Fix | Delete
# Converts the set to an array. The order of elements is uncertain.
[190] Fix | Delete
#
[191] Fix | Delete
# Set[1, 2].to_a #=> [1, 2]
[192] Fix | Delete
# Set[1, 'c', :s].to_a #=> [1, "c", :s]
[193] Fix | Delete
def to_a
[194] Fix | Delete
@hash.keys
[195] Fix | Delete
end
[196] Fix | Delete
[197] Fix | Delete
# Returns self if no arguments are given. Otherwise, converts the
[198] Fix | Delete
# set to another with `klass.new(self, *args, &block)`.
[199] Fix | Delete
#
[200] Fix | Delete
# In subclasses, returns `klass.new(self, *args, &block)` unless
[201] Fix | Delete
# overridden.
[202] Fix | Delete
def to_set(klass = Set, *args, &block)
[203] Fix | Delete
return self if instance_of?(Set) && klass == Set && block.nil? && args.empty?
[204] Fix | Delete
klass.new(self, *args, &block)
[205] Fix | Delete
end
[206] Fix | Delete
[207] Fix | Delete
def flatten_merge(set, seen = Set.new) # :nodoc:
[208] Fix | Delete
set.each { |e|
[209] Fix | Delete
if e.is_a?(Set)
[210] Fix | Delete
if seen.include?(e_id = e.object_id)
[211] Fix | Delete
raise ArgumentError, "tried to flatten recursive Set"
[212] Fix | Delete
end
[213] Fix | Delete
[214] Fix | Delete
seen.add(e_id)
[215] Fix | Delete
flatten_merge(e, seen)
[216] Fix | Delete
seen.delete(e_id)
[217] Fix | Delete
else
[218] Fix | Delete
add(e)
[219] Fix | Delete
end
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
self
[223] Fix | Delete
end
[224] Fix | Delete
protected :flatten_merge
[225] Fix | Delete
[226] Fix | Delete
# Returns a new set that is a copy of the set, flattening each
[227] Fix | Delete
# containing set recursively.
[228] Fix | Delete
def flatten
[229] Fix | Delete
self.class.new.flatten_merge(self)
[230] Fix | Delete
end
[231] Fix | Delete
[232] Fix | Delete
# Equivalent to Set#flatten, but replaces the receiver with the
[233] Fix | Delete
# result in place. Returns nil if no modifications were made.
[234] Fix | Delete
def flatten!
[235] Fix | Delete
replace(flatten()) if any? { |e| e.is_a?(Set) }
[236] Fix | Delete
end
[237] Fix | Delete
[238] Fix | Delete
# Returns true if the set contains the given object.
[239] Fix | Delete
#
[240] Fix | Delete
# Note that <code>include?</code> and <code>member?</code> do not test member
[241] Fix | Delete
# equality using <code>==</code> as do other Enumerables.
[242] Fix | Delete
#
[243] Fix | Delete
# See also Enumerable#include?
[244] Fix | Delete
def include?(o)
[245] Fix | Delete
@hash[o]
[246] Fix | Delete
end
[247] Fix | Delete
alias member? include?
[248] Fix | Delete
[249] Fix | Delete
# Returns true if the set is a superset of the given set.
[250] Fix | Delete
def superset?(set)
[251] Fix | Delete
case
[252] Fix | Delete
when set.instance_of?(self.class) && @hash.respond_to?(:>=)
[253] Fix | Delete
@hash >= set.instance_variable_get(:@hash)
[254] Fix | Delete
when set.is_a?(Set)
[255] Fix | Delete
size >= set.size && set.all? { |o| include?(o) }
[256] Fix | Delete
else
[257] Fix | Delete
raise ArgumentError, "value must be a set"
[258] Fix | Delete
end
[259] Fix | Delete
end
[260] Fix | Delete
alias >= superset?
[261] Fix | Delete
[262] Fix | Delete
# Returns true if the set is a proper superset of the given set.
[263] Fix | Delete
def proper_superset?(set)
[264] Fix | Delete
case
[265] Fix | Delete
when set.instance_of?(self.class) && @hash.respond_to?(:>)
[266] Fix | Delete
@hash > set.instance_variable_get(:@hash)
[267] Fix | Delete
when set.is_a?(Set)
[268] Fix | Delete
size > set.size && set.all? { |o| include?(o) }
[269] Fix | Delete
else
[270] Fix | Delete
raise ArgumentError, "value must be a set"
[271] Fix | Delete
end
[272] Fix | Delete
end
[273] Fix | Delete
alias > proper_superset?
[274] Fix | Delete
[275] Fix | Delete
# Returns true if the set is a subset of the given set.
[276] Fix | Delete
def subset?(set)
[277] Fix | Delete
case
[278] Fix | Delete
when set.instance_of?(self.class) && @hash.respond_to?(:<=)
[279] Fix | Delete
@hash <= set.instance_variable_get(:@hash)
[280] Fix | Delete
when set.is_a?(Set)
[281] Fix | Delete
size <= set.size && all? { |o| set.include?(o) }
[282] Fix | Delete
else
[283] Fix | Delete
raise ArgumentError, "value must be a set"
[284] Fix | Delete
end
[285] Fix | Delete
end
[286] Fix | Delete
alias <= subset?
[287] Fix | Delete
[288] Fix | Delete
# Returns true if the set is a proper subset of the given set.
[289] Fix | Delete
def proper_subset?(set)
[290] Fix | Delete
case
[291] Fix | Delete
when set.instance_of?(self.class) && @hash.respond_to?(:<)
[292] Fix | Delete
@hash < set.instance_variable_get(:@hash)
[293] Fix | Delete
when set.is_a?(Set)
[294] Fix | Delete
size < set.size && all? { |o| set.include?(o) }
[295] Fix | Delete
else
[296] Fix | Delete
raise ArgumentError, "value must be a set"
[297] Fix | Delete
end
[298] Fix | Delete
end
[299] Fix | Delete
alias < proper_subset?
[300] Fix | Delete
[301] Fix | Delete
# Returns 0 if the set are equal,
[302] Fix | Delete
# -1 / +1 if the set is a proper subset / superset of the given set,
[303] Fix | Delete
# or nil if they both have unique elements.
[304] Fix | Delete
def <=>(set)
[305] Fix | Delete
return unless set.is_a?(Set)
[306] Fix | Delete
[307] Fix | Delete
case size <=> set.size
[308] Fix | Delete
when -1 then -1 if proper_subset?(set)
[309] Fix | Delete
when +1 then +1 if proper_superset?(set)
[310] Fix | Delete
else 0 if self.==(set)
[311] Fix | Delete
end
[312] Fix | Delete
end
[313] Fix | Delete
[314] Fix | Delete
# Returns true if the set and the given set have at least one
[315] Fix | Delete
# element in common.
[316] Fix | Delete
#
[317] Fix | Delete
# Set[1, 2, 3].intersect? Set[4, 5] #=> false
[318] Fix | Delete
# Set[1, 2, 3].intersect? Set[3, 4] #=> true
[319] Fix | Delete
def intersect?(set)
[320] Fix | Delete
set.is_a?(Set) or raise ArgumentError, "value must be a set"
[321] Fix | Delete
if size < set.size
[322] Fix | Delete
any? { |o| set.include?(o) }
[323] Fix | Delete
else
[324] Fix | Delete
set.any? { |o| include?(o) }
[325] Fix | Delete
end
[326] Fix | Delete
end
[327] Fix | Delete
[328] Fix | Delete
# Returns true if the set and the given set have no element in
[329] Fix | Delete
# common. This method is the opposite of `intersect?`.
[330] Fix | Delete
#
[331] Fix | Delete
# Set[1, 2, 3].disjoint? Set[3, 4] #=> false
[332] Fix | Delete
# Set[1, 2, 3].disjoint? Set[4, 5] #=> true
[333] Fix | Delete
def disjoint?(set)
[334] Fix | Delete
!intersect?(set)
[335] Fix | Delete
end
[336] Fix | Delete
[337] Fix | Delete
# Calls the given block once for each element in the set, passing
[338] Fix | Delete
# the element as parameter. Returns an enumerator if no block is
[339] Fix | Delete
# given.
[340] Fix | Delete
def each(&block)
[341] Fix | Delete
block or return enum_for(__method__) { size }
[342] Fix | Delete
@hash.each_key(&block)
[343] Fix | Delete
self
[344] Fix | Delete
end
[345] Fix | Delete
[346] Fix | Delete
# Adds the given object to the set and returns self. Use `merge` to
[347] Fix | Delete
# add many elements at once.
[348] Fix | Delete
#
[349] Fix | Delete
# Set[1, 2].add(3) #=> #<Set: {1, 2, 3}>
[350] Fix | Delete
# Set[1, 2].add([3, 4]) #=> #<Set: {1, 2, [3, 4]}>
[351] Fix | Delete
# Set[1, 2].add(2) #=> #<Set: {1, 2}>
[352] Fix | Delete
def add(o)
[353] Fix | Delete
@hash[o] = true
[354] Fix | Delete
self
[355] Fix | Delete
end
[356] Fix | Delete
alias << add
[357] Fix | Delete
[358] Fix | Delete
# Adds the given object to the set and returns self. If the
[359] Fix | Delete
# object is already in the set, returns nil.
[360] Fix | Delete
#
[361] Fix | Delete
# Set[1, 2].add?(3) #=> #<Set: {1, 2, 3}>
[362] Fix | Delete
# Set[1, 2].add?([3, 4]) #=> #<Set: {1, 2, [3, 4]}>
[363] Fix | Delete
# Set[1, 2].add?(2) #=> nil
[364] Fix | Delete
def add?(o)
[365] Fix | Delete
add(o) unless include?(o)
[366] Fix | Delete
end
[367] Fix | Delete
[368] Fix | Delete
# Deletes the given object from the set and returns self. Use
[369] Fix | Delete
# `subtract` to delete many items at once.
[370] Fix | Delete
def delete(o)
[371] Fix | Delete
@hash.delete(o)
[372] Fix | Delete
self
[373] Fix | Delete
end
[374] Fix | Delete
[375] Fix | Delete
# Deletes the given object from the set and returns self. If the
[376] Fix | Delete
# object is not in the set, returns nil.
[377] Fix | Delete
def delete?(o)
[378] Fix | Delete
delete(o) if include?(o)
[379] Fix | Delete
end
[380] Fix | Delete
[381] Fix | Delete
# Deletes every element of the set for which block evaluates to
[382] Fix | Delete
# true, and returns self. Returns an enumerator if no block is
[383] Fix | Delete
# given.
[384] Fix | Delete
def delete_if
[385] Fix | Delete
block_given? or return enum_for(__method__) { size }
[386] Fix | Delete
# @hash.delete_if should be faster, but using it breaks the order
[387] Fix | Delete
# of enumeration in subclasses.
[388] Fix | Delete
select { |o| yield o }.each { |o| @hash.delete(o) }
[389] Fix | Delete
self
[390] Fix | Delete
end
[391] Fix | Delete
[392] Fix | Delete
# Deletes every element of the set for which block evaluates to
[393] Fix | Delete
# false, and returns self. Returns an enumerator if no block is
[394] Fix | Delete
# given.
[395] Fix | Delete
def keep_if
[396] Fix | Delete
block_given? or return enum_for(__method__) { size }
[397] Fix | Delete
# @hash.keep_if should be faster, but using it breaks the order of
[398] Fix | Delete
# enumeration in subclasses.
[399] Fix | Delete
reject { |o| yield o }.each { |o| @hash.delete(o) }
[400] Fix | Delete
self
[401] Fix | Delete
end
[402] Fix | Delete
[403] Fix | Delete
# Replaces the elements with ones returned by `collect()`.
[404] Fix | Delete
# Returns an enumerator if no block is given.
[405] Fix | Delete
def collect!
[406] Fix | Delete
block_given? or return enum_for(__method__) { size }
[407] Fix | Delete
set = self.class.new
[408] Fix | Delete
each { |o| set << yield(o) }
[409] Fix | Delete
replace(set)
[410] Fix | Delete
end
[411] Fix | Delete
alias map! collect!
[412] Fix | Delete
[413] Fix | Delete
# Equivalent to Set#delete_if, but returns nil if no changes were
[414] Fix | Delete
# made. Returns an enumerator if no block is given.
[415] Fix | Delete
def reject!(&block)
[416] Fix | Delete
block or return enum_for(__method__) { size }
[417] Fix | Delete
n = size
[418] Fix | Delete
delete_if(&block)
[419] Fix | Delete
self if size != n
[420] Fix | Delete
end
[421] Fix | Delete
[422] Fix | Delete
# Equivalent to Set#keep_if, but returns nil if no changes were
[423] Fix | Delete
# made. Returns an enumerator if no block is given.
[424] Fix | Delete
def select!(&block)
[425] Fix | Delete
block or return enum_for(__method__) { size }
[426] Fix | Delete
n = size
[427] Fix | Delete
keep_if(&block)
[428] Fix | Delete
self if size != n
[429] Fix | Delete
end
[430] Fix | Delete
[431] Fix | Delete
# Equivalent to Set#select!
[432] Fix | Delete
alias filter! select!
[433] Fix | Delete
[434] Fix | Delete
# Merges the elements of the given enumerable object to the set and
[435] Fix | Delete
# returns self.
[436] Fix | Delete
def merge(enum)
[437] Fix | Delete
if enum.instance_of?(self.class)
[438] Fix | Delete
@hash.update(enum.instance_variable_get(:@hash))
[439] Fix | Delete
else
[440] Fix | Delete
do_with_enum(enum) { |o| add(o) }
[441] Fix | Delete
end
[442] Fix | Delete
[443] Fix | Delete
self
[444] Fix | Delete
end
[445] Fix | Delete
[446] Fix | Delete
# Deletes every element that appears in the given enumerable object
[447] Fix | Delete
# and returns self.
[448] Fix | Delete
def subtract(enum)
[449] Fix | Delete
do_with_enum(enum) { |o| delete(o) }
[450] Fix | Delete
self
[451] Fix | Delete
end
[452] Fix | Delete
[453] Fix | Delete
# Returns a new set built by merging the set and the elements of the
[454] Fix | Delete
# given enumerable object.
[455] Fix | Delete
#
[456] Fix | Delete
# Set[1, 2, 3] | Set[2, 4, 5] #=> #<Set: {1, 2, 3, 4, 5}>
[457] Fix | Delete
# Set[1, 5, 'z'] | (1..6) #=> #<Set: {1, 5, "z", 2, 3, 4, 6}>
[458] Fix | Delete
def |(enum)
[459] Fix | Delete
dup.merge(enum)
[460] Fix | Delete
end
[461] Fix | Delete
alias + |
[462] Fix | Delete
alias union |
[463] Fix | Delete
[464] Fix | Delete
# Returns a new set built by duplicating the set, removing every
[465] Fix | Delete
# element that appears in the given enumerable object.
[466] Fix | Delete
#
[467] Fix | Delete
# Set[1, 3, 5] - Set[1, 5] #=> #<Set: {3}>
[468] Fix | Delete
# Set['a', 'b', 'z'] - ['a', 'c'] #=> #<Set: {"b", "z"}>
[469] Fix | Delete
def -(enum)
[470] Fix | Delete
dup.subtract(enum)
[471] Fix | Delete
end
[472] Fix | Delete
alias difference -
[473] Fix | Delete
[474] Fix | Delete
# Returns a new set containing elements common to the set and the
[475] Fix | Delete
# given enumerable object.
[476] Fix | Delete
#
[477] Fix | Delete
# Set[1, 3, 5] & Set[3, 2, 1] #=> #<Set: {3, 1}>
[478] Fix | Delete
# Set['a', 'b', 'z'] & ['a', 'b', 'c'] #=> #<Set: {"a", "b"}>
[479] Fix | Delete
def &(enum)
[480] Fix | Delete
n = self.class.new
[481] Fix | Delete
if enum.is_a?(Set)
[482] Fix | Delete
if enum.size > size
[483] Fix | Delete
each { |o| n.add(o) if enum.include?(o) }
[484] Fix | Delete
else
[485] Fix | Delete
enum.each { |o| n.add(o) if include?(o) }
[486] Fix | Delete
end
[487] Fix | Delete
else
[488] Fix | Delete
do_with_enum(enum) { |o| n.add(o) if include?(o) }
[489] Fix | Delete
end
[490] Fix | Delete
n
[491] Fix | Delete
end
[492] Fix | Delete
alias intersection &
[493] Fix | Delete
[494] Fix | Delete
# Returns a new set containing elements exclusive between the set
[495] Fix | Delete
# and the given enumerable object. `(set ^ enum)` is equivalent to
[496] Fix | Delete
# `((set | enum) - (set & enum))`.
[497] Fix | Delete
#
[498] Fix | Delete
# Set[1, 2] ^ Set[2, 3] #=> #<Set: {3, 1}>
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function