Skip to content

Instantly share code, notes, and snippets.

@idimitrov07
Created June 19, 2016 15:06
Show Gist options
  • Select an option

  • Save idimitrov07/7aaf6154d82aa52e87400a3d5924277c to your computer and use it in GitHub Desktop.

Select an option

Save idimitrov07/7aaf6154d82aa52e87400a3d5924277c to your computer and use it in GitHub Desktop.
$VERBOSE = nil # We'll explain this at the end of the lesson.
require 'prime' # This is a module. We'll cover these soon!
def first_n_primes(n)
unless n.is_a? Integer
return "n must be an integer."
end
if n <= 0
return "n must be greater than 0."
end
prime_array = [] if prime_array.nil?
prime = Prime.new
for num in (1..n)
prime_array.push(prime.next)
end
return prime_array
end
first_n_primes(10)
# refactored
$VERBOSE = nil # We'll explain this at the end of the lesson.
require 'prime' # This is a module. We'll cover these soon!
def first_n_primes(n)
return "n must be an integer." unless n.is_a? Integer
return "n must be greater than 0." if n <= 0
prime_array ||= []
prime = Prime.new
n.times { prime_array << prime.next }
prime_array
end
first_n_primes(10)
# refactored more
require 'prime'
def first_n_primes(n)
# Check for correct input!
"n must be an integer" unless n.is_a? Integer
"n must be greater than 0" if n <= 0
# The Ruby 1.9 Prime class makes the array automatically!
prime = Prime.instance
prime.first n
end
first_n_primes(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment