Class Prime::PseudoPrimeGenerator
In: lib/backports/1.9.1/stdlib/prime.rb
Parent: Object

An abstract class for enumerating pseudo-prime numbers.

Concrete subclasses should override succ, next, rewind.

Methods

each   new   next   rewind   succ   upper_bound   upper_bound=   with_object  

Included Modules

Enumerable

External Aliases

each_with_index -> with_index
  see Enumeratorwith_index.

Public Class methods

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 227
227:     def initialize(ubound = nil)
228:       @ubound = ubound
229:     end

Public Instance methods

Iterates the given block for each prime numbers.

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 259
259:     def each(&block)
260:       return self.dup unless block
261:       if @ubound
262:         last_value = nil
263:         loop do
264:           prime = succ
265:           break last_value if prime > @ubound
266:           last_value = block.call(prime)
267:         end
268:       else
269:         loop do
270:           block.call(succ)
271:         end
272:       end
273:     end

alias of succ.

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 247
247:     def next
248:       raise NotImplementedError, "need to define `next'"
249:     end

Rewinds the internal position for enumeration.

See Enumeratorrewind.

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 254
254:     def rewind
255:       raise NotImplementedError, "need to define `rewind'"
256:     end

returns the next pseudo-prime number, and move the internal position forward.

PseudoPrimeGeneratorsucc raises NotImplementedError.

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 242
242:     def succ
243:       raise NotImplementedError, "need to define `succ'"
244:     end

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 234
234:     def upper_bound
235:       @ubound
236:     end

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 231
231:     def upper_bound=(ubound)
232:       @ubound = ubound
233:     end

see Enumeratorwith_object.

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 279
279:     def with_object(obj)
280:       return enum_for(:with_object) unless block_given?
281:       each do |prime|
282:         yield prime, obj
283:       end
284:     end

[Validate]