Project

General

Profile

Feature #11710 » non_mutating_set_merge_method.diff

tiegz (Tieg Zaharia), 11/18/2015 05:13 PM

View differences:

lib/set.rb
if block
do_with_enum(enum) { |o| add(block[o]) }
else
merge(enum)
merge!(enum)
end
end
......
else
do_with_enum(enum) # make sure enum is enumerable before calling clear
clear
merge(enum)
merge!(enum)
end
end
......
self if size != n
end
def merge(enum)
dup.merge!(enum)
end
# Merges the elements of the given enumerable object to the set and
# returns self.
def merge(enum)
def merge!(enum)
if enum.instance_of?(self.class)
@hash.update(enum.instance_variable_get(:@hash))
else
......
# Returns a new set built by merging the set and the elements of the
# given enumerable object.
def |(enum)
dup.merge(enum)
dup.merge!(enum)
end
alias + | ##
alias union | ##
test/test_set.rb
set = Set[1,2,3]
ret = set.merge([2,4,6])
assert_not_same(set, ret)
assert_equal(Set[1,2,3,4,6], ret)
end
def test_merge!
set = Set[1,2,3]
ret = set.merge!([2,4,6])
assert_same(set, ret)
assert_equal(Set[1,2,3,4,6], set)
end
(1-1/2)