# 4 Less is More Is There Here not a contradiction?

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
ERROR MESSAGE :"You might have picked the wrong ‘return’! You need the first two, but not the last one."
HINT MESSAGE:“Hint: it’s the last one!
The first two return statements are necessary. We may want to return early if the input isn’t a positive number. The only way to return early is with an explicit return statement.”

We’ve learned in former lessons that it is the if return that is not necessary , isn’t it ?

`````` 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
for num in (1..n)
prime_array.push(prime.next)
end
reprime_array

``````

#2

This does not look right. The return value should be, `prime_array`.

#3

It still doesn’t work .

The hint message is the contrary of the error message ???

HINT MESSAGE :“Hint: it’s the last one!
The first two return statements are necessary. We may want to return early if the input isn’t a positive number. The only way to return early is with an explicit return statement.”

ERROR MESSAGE:“You might have picked the wrong ‘return’! You need the first two, but not the last one.”

#4

I reset that exercise, removed the final return keyword and got the same error message. Since we lack a command line console my next step was test a repl of it.

``````\$VERBOSE = nil
require 'prime'

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 ||= []

prime = Prime.new
for num in (1..n)
prime_array.push(prime.next)
end
return prime_array
end

first_n_primes(10)
``````

This was the immediate result…

``````ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]

private method `new' called for Prime:Class
(repl):16:in `first_n_primes'
(repl):23:in `<main>'
``````

Turns out `new` has been aliased to `instance` which comes packaged as an array so does not need to be initialized. Let’s make that change and see what happens.

``````\$VERBOSE = nil
require 'prime'

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 ||= []

prime = Prime.instance
for num in (1..n)
prime_array.push(prime.next)
end
return prime_array
end

first_n_primes(10)
``````
``````undefined method `next' for #<Prime:0x0055da73492f48>
(repl):18:in `block in first_n_primes'
(repl):17:in `each'
(repl):17:in `first_n_primes'
(repl):23:in `<main>'
``````

This before even the refactored array_push method. Turns out the array is unnecessary. The instance is a generator. Paring down…

``````require 'prime
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 = Prime.instance
return prime.first n
end

first_n_primes(10)    #=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
``````

Now, removing the last return gives,

``````require 'prime'
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 = Prime.instance
prime.first n
end

first_n_primes(10)    #=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
``````

Which passes muster.

#5

This should be the clear way for this function. The Prime instance methods ( .first ) can be called as its Class method.
Ruby doc

``````require 'prime'
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.first n
end

first_n_primes(10)    #=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
``````

#6

That was going to be the next thing to bring up…

``````def first_n_primes(n):
"n must be an integer." unless n.is_a? Integer
"n must be greater than 0." unless n > 0
Prime.first n
end
``````

However, it does not come up in this module, and only arose because of the recent issues with the Ruby version. The module will need to be revised at some point.

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.