Revision
5266
Author
watson1978@gmail.com
Date
2011-03-08 05:39:22 -0800 (Tue, 08 Mar 2011)

Log Message

 Implements the Set#{keep_if, select!}.

Modified Paths

Diff

Modified: MacRuby/trunk/lib/set.rb (5265 => 5266)


--- MacRuby/trunk/lib/set.rb	2011-03-08 04:49:56 UTC (rev 5265)
+++ MacRuby/trunk/lib/set.rb	2011-03-08 13:39:22 UTC (rev 5266)
@@ -266,6 +266,14 @@
     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 @@
     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 @@
 	    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

Modified: MacRuby/trunk/spec/frozen/tags/macruby/library/set/keep_if_tags.txt (5265 => 5266)


--- MacRuby/trunk/spec/frozen/tags/macruby/library/set/keep_if_tags.txt	2011-03-08 04:49:56 UTC (rev 5265)
+++ MacRuby/trunk/spec/frozen/tags/macruby/library/set/keep_if_tags.txt	2011-03-08 13:39:22 UTC (rev 5266)
@@ -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

Modified: MacRuby/trunk/spec/frozen/tags/macruby/library/set/select_tags.txt (5265 => 5266)


--- MacRuby/trunk/spec/frozen/tags/macruby/library/set/select_tags.txt	2011-03-08 04:49:56 UTC (rev 5265)
+++ MacRuby/trunk/spec/frozen/tags/macruby/library/set/select_tags.txt	2011-03-08 13:39:22 UTC (rev 5266)
@@ -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