[前][次][番号順一覧][スレッド一覧]

ruby-changes:7614

From: yugui <ko1@a...>
Date: Thu, 4 Sep 2008 23:10:10 +0900 (JST)
Subject: [ruby-changes:7614] Ruby:r19135 (trunk): * lib/prime.rb (Prime::OldCompatibility#each): added compatibility to

yugui	2008-09-04 23:09:52 +0900 (Thu, 04 Sep 2008)

  New Revision: 19135

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19135

  Log:
    * lib/prime.rb (Prime::OldCompatibility#each): added compatibility to
        Ruby 1.8.7.
      (Prime#each): added more rdocs.
      (Prime#each): remembers the last value of the given block.

  Modified files:
    trunk/ChangeLog
    trunk/lib/prime.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 19134)
+++ ChangeLog	(revision 19135)
@@ -1,3 +1,10 @@
+Thu Sep  4 23:05:54 2008  Yuki Sonoda (Yugui)  <yugui@y...>
+
+	* lib/prime.rb (Prime::OldCompatibility#each): added compatibility to 
+	    Ruby 1.8.7.
+	  (Prime#each): added more rdocs.
+	  (Prime#each): remembers the last value of the given block.
+
 Thu Sep  4 21:53:58 2008  Tanaka Akira  <akr@f...>
 
 	* transcode.c (econv_init): accept an integer as 3rd argument as well.
Index: lib/prime.rb
===================================================================
--- lib/prime.rb	(revision 19134)
+++ lib/prime.rb	(revision 19135)
@@ -91,19 +91,6 @@
     warn "Prime::new is obsolete. use Prime::instance or class methods of Prime."
   end
 
-  module OldCompatibility
-    def succ
-      @generator.succ
-    end
-    alias next succ
-
-    def each(&block)
-      loop do
-	yield succ
-      end
-    end
-  end
-
   class<<self
     extend Forwardable
     include Enumerable
@@ -137,6 +124,14 @@
   # +ubound+::
   #   Upper bound of prime numbers. The iterator stops after 
   #   yields all prime numbers p <= +ubound+.
+  #
+  # == Note
+  # +Prime+.+new+ returns a object extended by +Prime+::+OldCompatibility+
+  # in order to compatibility to Ruby 1.9, and +Prime+#each is overwritten
+  # by +Prime+::+OldCompatibility+#+each+.
+  #
+  # +Prime+.+new+ is now obsolete. Use +Prime+.+instance+.+each+ or simply
+  # +Prime+.+each+.
   def each(ubound = nil, generator = EratosthenesGenerator.new, &block)
     generator.upper_bound = ubound
     generator.each(&block)
@@ -254,18 +249,18 @@
     end
 
     # Iterates the given block for each prime numbers.
-    # +ubound+:: 
     def each(&block)
       return self.dup unless block
       if @ubound
+	last_value = nil
 	loop do
-	  p = succ
-	  break if p > @ubound
-	  block.call p
+	  prime = succ
+	  break last_value if prime > @ubound
+	  last_value = block.call(prime)
 	end
       else
 	loop do
-	  block.call succ
+	  block.call(succ)
 	end
       end
     end
@@ -351,7 +346,7 @@
 
 
 
-  # An implementation of prime table by trial division method.
+  # Internal use. An implementation of prime table by trial division method.
   class TrialDivision
     include Singleton
 
@@ -399,7 +394,7 @@
     end
   end
 
-  # An implementation of eratosthenes's sieve
+  # Internal use. An implementation of eratosthenes's sieve
   class EratosthenesSieve
     include Singleton
 
@@ -443,4 +438,24 @@
       end
     end
   end
+
+  # Provides a +Prime+ object with compatibility to Ruby 1.8 when instanciated via +Prime+.+new+.
+  module OldCompatibility
+    # Returns the next prime number and forwards internal pointer.
+    def succ
+      @generator.succ
+    end
+    alias next succ
+
+    # Overwrites Prime#each.
+    #
+    # Iterates the given block over all prime numbers. Note that enumeration starts from
+    # the current position of internal pointer, not rewinded.
+    def each(&block)
+      return @generator.dup unless block_given?
+      loop do
+	yield succ
+      end
+    end
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]