Created
August 27, 2014 03:35
-
-
Save ruanwz/9b9ec5f03592730dd345 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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