4 - Less is More


#1

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

#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

Mine still didn't work although I removed the return. Mine looks exactly like the one above.


#4

We will need to examine your code. Please post it, as well as a link to this lesson. Thank you.


#5

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)


#6

This is the possible problem. The response is 1..10 when it should be an array returned.

    end
    prime_array
end

#7

Why are these implicit returns better than explicit?


#8

I'll defer to what you will find in the SERP:

https://www.google.ca/search?q=ruby+implicit+return


#9

5 posts were split to a new topic: Haven't quite been able to determine the solution


#14