I am starting to learn Ruby.
This is pretty much the first thing I have coded. It's very simple; it's a prime number calculator. Since this is my first Ruby code, I would like some review on the following:
- Adherence to Ruby standards
- Is it done in the Ruby way (the way a Rubyist would have coded it)?
- Adherence to Ruby naming conventions
What I am not looking for is a review on the prime number algorithm. I know there are more efficient ones out there.
class PrimeNumber def is_prime_kata(number) if number == 1 then return false end max = Math.sqrt(number) (2..max).any? do |i| if number % i == 0 then return false end end true end end
require 'test/unit' require 'set' require_relative 'prime_numbers.rb' class PrimeNumberTest < Test::Unit::TestCase def test_is_prime primeNumbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] primeNumber = PrimeNumber.new for i in primeNumbers result = primeNumber.is_prime_kata(i) assert_equal(true, result) end primeNumbersSet = Set.new(primeNumbers) allNumbersSet = Set.new(1..100) nonPrimeNumbersSet = allNumbersSet - primeNumbersSet for i in nonPrimeNumbersSet result = primeNumber.is_prime_kata(i) assert_equal(false, result) end end end