[macruby-changes] [5266] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Tue Mar 8 05:39:22 PST 2011


Revision: 5266
          http://trac.macosforge.org/projects/ruby/changeset/5266
Author:   watson1978 at gmail.com
Date:     2011-03-08 05:39:22 -0800 (Tue, 08 Mar 2011)
Log Message:
-----------
 Implements the Set#{keep_if, select!}.

Modified Paths:
--------------
    MacRuby/trunk/lib/set.rb
    MacRuby/trunk/spec/frozen/tags/macruby/library/set/keep_if_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/library/set/select_tags.txt

Modified: MacRuby/trunk/lib/set.rb
===================================================================
--- 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
===================================================================
--- 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
===================================================================
--- 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20110308/5bf45da2/attachment.html>


More information about the macruby-changes mailing list