Maintenance of Ruby 2.0.0 will end on February 24, 2016. Read more

In Files

  • prime.rb


Generates all integer which are greater than 2 and are not divided by 2 nor 3.

This is a pseudo-prime generator, suitable on checking primality of a integer by brute force method.

Public Class Methods

new() click to toggle source
               # File prime.rb, line 346
def initialize
  @prime = 1
  @step = nil

Public Instance Methods

next() click to toggle source
Alias for: succ
rewind() click to toggle source
               # File prime.rb, line 368
def rewind
succ() click to toggle source
               # File prime.rb, line 352
def succ
  loop do
    if (@step)
      @prime += @step
      @step = 6 - @step
      case @prime
      when 1; @prime = 2
      when 2; @prime = 3
      when 3; @prime = 5; @step = 2
    return @prime
Also aliased as: next

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit

blog comments powered by Disqus