1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/set.rb: Make Set#each and SortedSet#each generate a sized

enumerator. [GH-931] by kachick (Kenichi Kamiya)

* test/test_set.rb: Import tests from Set into SortedSet. [GH-931]
  by kachick (Kenichi Kamiya)

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50908 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2015-06-15 05:37:38 +00:00
parent bab5bf0c79
commit 9f2e41b7e0
3 changed files with 37 additions and 2 deletions

View file

@ -1,3 +1,11 @@
Mon Jun 15 14:33:02 2015 Akinori MUSHA <knu@iDaemons.org>
* lib/set.rb: Make Set#each and SortedSet#each generate a sized
enumerator. [GH-931] by kachick (Kenichi Kamiya)
* test/test_set.rb: Import tests from Set into SortedSet. [GH-931]
by kachick (Kenichi Kamiya)
Mon Jun 15 02:26:34 2015 NARUSE, Yui <naruse@ruby-lang.org>
* lib/net/http.rb (Net::HTTP#connect): use connect_nonblock and

View file

@ -284,7 +284,7 @@ class Set
# the element as parameter. Returns an enumerator if no block is
# given.
def each(&block)
block or return enum_for(__method__)
block or return enum_for(__method__) { size }
@hash.each_key(&block)
self
end
@ -663,7 +663,7 @@ class SortedSet < Set
end
def each(&block)
block or return enum_for(__method__)
block or return enum_for(__method__) { size }
to_a.each(&block)
self
end

View file

@ -364,6 +364,10 @@ class TC_Set < Test::Unit::TestCase
ary.empty? or raise "forgotten elements: #{ary.join(', ')}"
}
assert_equal(6, e.size)
set << 42
assert_equal(7, e.size)
end
def test_add
@ -669,6 +673,29 @@ class TC_SortedSet < Test::Unit::TestCase
assert_equal(['four', 'one', 'three', 'two'], s.to_a)
assert_equal(['four', 'one', 'three', 'two'], a)
end
def test_each
ary = [1,3,5,7,10,20]
set = SortedSet.new(ary)
ret = set.each { |o| }
assert_same(set, ret)
e = set.each
assert_instance_of(Enumerator, e)
assert_nothing_raised {
set.each { |o|
ary.delete(o) or raise "unexpected element: #{o}"
}
ary.empty? or raise "forgotten elements: #{ary.join(', ')}"
}
assert_equal(6, e.size)
set << 42
assert_equal(7, e.size)
end
end
class TC_Enumerable < Test::Unit::TestCase