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

* lib/set.rb (Set#delete_if, Set#keep_if): Make Set#delete_if and

Set#keep_if more space and time efficient by avoiding to_a.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40819 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2013-05-18 17:48:18 +00:00
parent 041224017c
commit 2849ee5d18
3 changed files with 19 additions and 2 deletions

View file

@ -1,3 +1,8 @@
Sun May 19 02:46:32 2013 Akinori MUSHA <knu@iDaemons.org>
* lib/set.rb (Set#delete_if, Set#keep_if): Make Set#delete_if and
Set#keep_if more space and time efficient by avoiding to_a.
Sun May 19 02:33:09 2013 Akinori MUSHA <knu@iDaemons.org>
* misc/ruby-electric.el (ruby-electric-setup-keymap): Make

View file

@ -272,7 +272,7 @@ class Set
# true, and returns self.
def delete_if
block_given? or return enum_for(__method__)
to_a.each { |o| @hash.delete(o) if yield(o) }
select(&proc).each(&@hash.method(:delete))
self
end
@ -280,7 +280,7 @@ class Set
# false, and returns self.
def keep_if
block_given? or return enum_for(__method__)
to_a.each { |o| @hash.delete(o) unless yield(o) }
reject(&proc).each(&@hash.method(:delete))
self
end

View file

@ -368,6 +368,18 @@ class TC_Set < Test::Unit::TestCase
assert_equal(Set[1,2,4,5,7,8,10], set)
end
def test_keep_if
set = Set.new(1..10)
ret = set.keep_if { |i| i <= 10 }
assert_same(set, ret)
assert_equal(Set.new(1..10), set)
set = Set.new(1..10)
ret = set.keep_if { |i| i % 3 != 0 }
assert_same(set, ret)
assert_equal(Set[1,2,4,5,7,8,10], set)
end
def test_collect!
set = Set[1,2,3,'a','b','c',-1..1,2..4]