From 645f25356a4455ccac6aea19b9c7e73bdee9d960 Mon Sep 17 00:00:00 2001 From: yugui Date: Thu, 4 Sep 2008 14:09:52 +0000 Subject: * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19135 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ lib/prime.rb | 55 +++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5d8b0a8ea8..80d92720e6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Thu Sep 4 23:05:54 2008 Yuki Sonoda (Yugui) + + * 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 * transcode.c (econv_init): accept an integer as 3rd argument as well. diff --git a/lib/prime.rb b/lib/prime.rb index be1c8b42a0..dd1190a0c7 100644 --- a/lib/prime.rb +++ b/lib/prime.rb @@ -91,19 +91,6 @@ class Prime 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< @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 @@ class Prime - # 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 @@ class Prime end end - # An implementation of eratosthenes's sieve + # Internal use. An implementation of eratosthenes's sieve class EratosthenesSieve include Singleton @@ -443,4 +438,24 @@ class Prime 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 -- cgit v1.2.3