This works, but I don’t understand why. All I did was remove the word “return.” I’m not sure how simply re-stating the name of the array variable “prime_array” does anything.
prime_array ||= []
prime = Prime.new
for num in (1..n)
prime_array.push(prime.next)
end
prime_array //removed "return" here
1 Like
mtf
November 10, 2015, 2:45am
#2
It changes from explicit to implicit return. Ruby automatically returns the value or expression just preceding end
at the end of the function.
3 Likes
Mine still didn’t work although I removed the return. Mine looks exactly like the one above.
mtf
January 3, 2016, 11:00pm
#4
We will need to examine your code. Please post it, as well as a link to this lesson. Thank you.
Here is my entire code…$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 puts “n must be an integer.”
if n <= 0 puts “n must be greater than 0.”
prime_array ||=
prime = Prime.new
for num in (1…n)
prime_array.push(prime.next)
prime_array
end
end
first_n_primes(10)
mtf
January 4, 2016, 2:16am
#6
sabrown84:
prime_array
end
end
This is the possible problem. The response is 1..10
when it should be an array returned.
end
prime_array
end
Why are these implicit returns better than explicit?
mtf
January 18, 2016, 7:03pm
#8
mtf
split this topic
February 3, 2017, 10:43pm
#9