Skip to content

Instantly share code, notes, and snippets.

@ruanwz
Created August 27, 2014 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruanwz/9b9ec5f03592730dd345 to your computer and use it in GitHub Desktop.
Save ruanwz/9b9ec5f03592730dd345 to your computer and use it in GitHub Desktop.
# encoding: utf-8
class Prime
def initialize(n)
@n = n
end
def calculate
result = [1]
(2..@n).each do |n|
result << n if (2..n-1).all? do |factor|
n % factor != 0
end
end
result
end
end
if __FILE__ == $0
require 'test/unit'
require 'minitest/spec'
describe Prime do
it "accept a number" do
proc {
Prime.new
}.must_raise ArgumentError
end
it "calculate returns a list" do
Prime.new(5).calculate.must_be_instance_of Array
end
it "returns a list includes 1" do
Prime.new(5).calculate.must_include 1
end
it "returns a list includes 1, 2, 3, 5, 7 when input argument is 7" do
Prime.new(7).calculate.must_equal [1,2,3,5,7]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment