summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-09 09:20:43 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-09 09:20:43 +0000
commita4187cb5caf0cf63a8d4d6f6a742f5a0035890ab (patch)
treeee0256662afb8bc4b172c2e844686cca346a6ac3
parent14c6cd2254c48ac18feda78b5c1c87f6d102f408 (diff)
* 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/branches/ruby_1_8_7@17051 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog10
-rw-r--r--lib/set.rb32
2 files changed, 39 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index fe0c2cf..07ade03 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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 03:28:05 2008 Akinori MUSHA <knu@iDaemons.org>
* ext/zlib/zlib.c (rb_deflate_initialize, Init_zlib): Fix up
diff --git a/lib/set.rb b/lib/set.rb
index 1d7232d..164aa42 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -234,7 +234,7 @@ class Set
# Deletes every element of the set for which block evaluates to
# true, and returns self.
def delete_if
- @hash.delete_if { |o,| yield(o) }
+ to_a.each { |o| @hash.delete(o) if yield(o) }
self
end
@@ -492,7 +492,7 @@ class SortedSet < Set
def delete_if
n = @hash.size
- @hash.delete_if { |o,| yield(o) }
+ super
@keys = nil if @hash.size != n
self
end
@@ -505,6 +505,7 @@ class SortedSet < Set
def each
block_given? or return enum_for(__method__)
to_a.each { |o| yield(o) }
+ self
end
def to_a
@@ -921,6 +922,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)
@@ -1167,11 +1171,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