Is there a way to refactor this method using map? (this intention of this method is to return an array of prime numbers between 1 and the max parameter)
def primes(max)
prime_arr = []
(1..max).each {|i| prime_arr << i if is_prime?(i)}
prime_arr
end
is_prime?(val) returns true or false.
if i change my method to:
def primes(max)
(1..max).map {|i| i if is_prime?(i)}
end
The returned array has a nil value when the code blocks fails.
p primes(5)
#=> [1, 2, 3, nil, 5]
I know what I have works, but i'd rather not declare the array and return the array specifically if there's a better way to do it