summaryrefslogtreecommitdiff
path: root/lib/set.rb
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-09 08:58:30 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-09 08:58:30 +0000
commit250dd0702111d5c2086f007b1b31ecc1e36ade40 (patch)
tree6ee7058e0c1ec8f8e1761ba7c6a3e03a06a79b69 /lib/set.rb
parent507f89d33fb2c3fb7180dfda1a27364819f67f32 (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/trunk@17045 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/set.rb')
-rw-r--r--lib/set.rb32
1 files changed, 29 insertions, 3 deletions
diff --git a/lib/set.rb b/lib/set.rb
index b6988b6..1a36b9c 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -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