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
87 87
    if block
88 88
      do_with_enum(enum) { |o| add(block[o]) }
89 89
    else
90
      merge(enum)
90
      merge!(enum)
91 91
    end
92 92
  end
93 93

  
......
155 155
    else
156 156
      do_with_enum(enum)  # make sure enum is enumerable before calling clear
157 157
      clear
158
      merge(enum)
158
      merge!(enum)
159 159
    end
160 160
  end
161 161

  
......
379 379
    self if size != n
380 380
  end
381 381

  
382
  def merge(enum)
383
    dup.merge!(enum)
384
  end
385

  
382 386
  # Merges the elements of the given enumerable object to the set and
383 387
  # returns self.
384
  def merge(enum)
388
  def merge!(enum)
385 389
    if enum.instance_of?(self.class)
386 390
      @hash.update(enum.instance_variable_get(:@hash))
387 391
    else
......
401 405
  # Returns a new set built by merging the set and the elements of the
402 406
  # given enumerable object.
403 407
  def |(enum)
404
    dup.merge(enum)
408
    dup.merge!(enum)
405 409
  end
406 410
  alias + |             ##
407 411
  alias union |         ##
test/test_set.rb
479 479
    set = Set[1,2,3]
480 480

  
481 481
    ret = set.merge([2,4,6])
482
    assert_not_same(set, ret)
483
    assert_equal(Set[1,2,3,4,6], ret)
484
  end
485

  
486
 def test_merge!
487
    set = Set[1,2,3]
488

  
489
    ret = set.merge!([2,4,6])
482 490
    assert_same(set, ret)
483 491
    assert_equal(Set[1,2,3,4,6], set)
484 492
  end