diff options
author | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-09-04 14:09:52 +0000 |
---|---|---|
committer | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-09-04 14:09:52 +0000 |
commit | 645f25356a4455ccac6aea19b9c7e73bdee9d960 () | |
tree | e37b28dab3a14b54fc86df47195024087202b322 /lib/prime.rb | |
parent | 0406b0f1547d33f1f1abb39524b846c36d5e234e (diff) |
* 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
-rw-r--r-- | lib/prime.rb | 55 |
1 files changed, 35 insertions, 20 deletions
@@ -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<<self extend Forwardable include Enumerable @@ -137,6 +124,14 @@ class Prime # +ubound+:: # Upper bound of prime numbers. The iterator stops after # yields all prime numbers p <= +ubound+. def each(ubound = nil, generator = EratosthenesGenerator.new, &block) generator.upper_bound = ubound generator.each(&block) @@ -254,18 +249,18 @@ class Prime end # Iterates the given block for each prime numbers. - # +ubound+:: def each(&block) return self.dup unless block if @ubound loop do - p = succ - break if p > @ubound - block.call p end else loop do - block.call succ end end end @@ -351,7 +346,7 @@ class Prime - # 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 class EratosthenesSieve include Singleton @@ -443,4 +438,24 @@ class Prime end end end end |