* lib/set.rb (Set#delete_if): Call to_a.

(SortedSet#delete_if, TC_SortedSet#test_sortedset): Use super to
  yield elements in sorted order; [ruby-core:17144] by Arthur
  Schreiber.
  (SortedSet#each, SortedSet#each, TC_Set#test_each)
  (TC_SortedSet#test_sortedset): Return self; [ruby-dev:35002] by
  Arthur Schreiber.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17045 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2008-06-09 08:58:30 +00:00
Родитель 507f89d33f
Коммит 250dd07021
2 изменённых файлов: 39 добавлений и 3 удалений

Просмотреть файл

@ -1,3 +1,13 @@
Mon Jun 9 17:56:30 2008 Akinori MUSHA <knu@iDaemons.org>
* lib/set.rb (Set#delete_if): Call to_a.
(SortedSet#delete_if, TC_SortedSet#test_sortedset): Use super to
yield elements in sorted order; [ruby-core:17144] by Arthur
Schreiber.
(SortedSet#each, SortedSet#each, TC_Set#test_each)
(TC_SortedSet#test_sortedset): Return self; [ruby-dev:35002] by
Arthur Schreiber.
Mon Jun 9 17:47:09 2008 Tanaka Akira <akr@fsij.org>
* io.c (fptr_finalize): close IO object if fd is already closed.

Просмотреть файл

@ -251,7 +251,7 @@ class Set
# true, and returns self.
def delete_if
block_given? or return enum_for(__method__)
@hash.delete_if { |o,| yield(o) }
to_a.each { |o| @hash.delete(o) if yield(o) }
self
end
@ -510,7 +510,7 @@ class SortedSet < Set
def delete_if
block_given? or return enum_for(__method__)
n = @hash.size
@hash.delete_if { |o,| yield(o) }
super
@keys = nil if @hash.size != n
self
end
@ -523,6 +523,7 @@ class SortedSet < Set
def each
block_given? or return enum_for(__method__)
to_a.each { |o| yield(o) }
self
end
def to_a
@ -936,6 +937,9 @@ class TC_Set < Test::Unit::TestCase
ary = [1,3,5,7,10,20]
set = Set.new(ary)
ret = set.each { |o| }
assert_same(set, ret)
e = set.each
assert_instance_of(Enumerable::Enumerator, e)
@ -1182,11 +1186,33 @@ class TC_SortedSet < Test::Unit::TestCase
assert_equal([-10,-8,-6,-4,-2], s.to_a)
prev = nil
s.each { |o| assert(prev < o) if prev; prev = o }
ret = s.each { |o| assert(prev < o) if prev; prev = o }
assert_not_nil(prev)
assert_same(s, ret)
s = SortedSet.new([2,1,3]) { |o| o * -2 }
assert_equal([-6,-4,-2], s.to_a)
s = SortedSet.new(['one', 'two', 'three', 'four'])
a = []
ret = s.delete_if { |o| a << o; o.start_with?('t') }
assert_same(s, ret)
assert_equal(['four', 'one'], s.to_a)
assert_equal(['four', 'one', 'three', 'two'], a)
s = SortedSet.new(['one', 'two', 'three', 'four'])
a = []
ret = s.reject! { |o| a << o; o.start_with?('t') }
assert_same(s, ret)
assert_equal(['four', 'one'], s.to_a)
assert_equal(['four', 'one', 'three', 'two'], a)
s = SortedSet.new(['one', 'two', 'three', 'four'])
a = []
ret = s.reject! { |o| a << o; false }
assert_same(nil, ret)
assert_equal(['four', 'one', 'three', 'two'], s.to_a)
assert_equal(['four', 'one', 'three', 'two'], a)
end
end