Skip to content
Browse files

Implements the Set#{keep_if, select!}.

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@5266 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent 895fd76 commit 8e115fdb6b66adc64c83608c428402074e5bef3a @Watson1978 Watson1978 committed
View
25 lib/set.rb
@@ -266,6 +266,14 @@ def delete_if
self
end
+ # Deletes every element of the set for which block evaluates to
+ # false, and returns self.
+ def keep_if
+ block_given? or return enum_for(__method__)
+ to_a.each { |o| @hash.delete(o) unless yield(o) }
+ self
+ end
+
# Replaces the elements with ones returned by collect().
def collect!
block_given? or return enum_for(__method__)
@@ -284,6 +292,15 @@ def reject!
size == n ? nil : self
end
+ # Equivalent to Set#keep_if, but returns nil if no changes were
+ # made.
+ def select!
+ block_given? or return enum_for(__method__)
+ n = size
+ keep_if { |o| yield(o) }
+ size == n ? nil : self
+ end
+
# Merges the elements of the given enumerable object to the set and
# returns self.
def merge(enum)
@@ -563,6 +580,14 @@ def delete_if
self
end
+ def keep_if
+ block_given? or return enum_for(__method__)
+ n = @hash.size
+ super
+ @keys = nil if @hash.size != n
+ self
+ end
+
def merge(enum)
@keys = nil
super
View
4 spec/frozen/tags/macruby/library/set/keep_if_tags.txt
@@ -1,4 +0,0 @@
-fails:Set#keep_if yields every element of self
-fails:Set#keep_if keeps every element from self for which the passed block returns true
-fails:Set#keep_if returns self
-fails:Set#keep_if returns an Enumerator when passed no block
View
5 spec/frozen/tags/macruby/library/set/select_tags.txt
@@ -1,5 +0,0 @@
-fails:Set#select! yields every element of self
-fails:Set#select! keeps every element from self for which the passed block returns true
-fails:Set#select! returns self when self was modified
-fails:Set#select! returns nil when self was not modified
-fails:Set#select! returns an Enumerator when passed no block

0 comments on commit 8e115fd

Please sign in to comment.
Something went wrong with that request. Please try again.