Unless? Is it right?

“n must be an integer” unless n.is_a? Integer

when n is an integer then (n.is_a? Integer) is true.
If I understood it right, unless works only when the expression returns false.
So in this case it should be the opposite, right? “n must not be an integer”

Please correct me I must be wrong somewhere!

1 Like

unless will execute "n must be an integer" if n.is_a? Integer is false (aka, n is not an integer).

Makes perfect sense to me: if n is not an integer, it means that the user didn’t give an integer, therefore as long as he doesn’t give us an integer, we will continue to tell him that n must be an integer.

1 Like

I didn’t understand it that way.
I thought it was a statement to say when it is an integer :smile:

Thanks! Makes perfect sense now :slight_smile:

It’s just a string object. And here we don’t even use puts or print so the user would probably not even know what we’re asking of him ^^.

I can’t seem to get mine to work, any thoughts on this one?
The one line logic seems correct as the error comes later.
The error I keep getting is apparently on line 18 below and the error is:
(ruby):18: syntax error, unexpected $end, expecting keyword_end
That is the end for the function, and the end on line 16 is the end of the loop, so what I can’t figure out what it’s expecting - to be sure i tried to add and extra ‘end’ and also tried to remove that ‘end’ on line 18 but no luck.
Any help would be super appreciated.


Here is the full block of code:

01$VERBOSE = nil # We’ll explain this at the end of the lesson.
02require ‘prime’ # This is a module. We’ll cover these soon!
04def first_n_primes(n)
07"n must be an integer" unless n.is_a? Integer
09 if n <= 0 then return “n must be greater than 0.”
11 prime_array ||=
13 prime = Prime.new
14 for num in (1…n)
15 prime_array.push(prime.next)
16 end
17 return prime_array

The problem is the IF statement on line 9. The way it is written, the interpreter is expecting an END for it. Two options are to add an END to line 9 like so:

if n<=0 then return "n must be greater than 0." end

or try re-writing the IF statement similar to how you wrote line 7:

return "n must be greater than 0." if n <= 0

$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
if n<=0 then return "n must be greater than 0."
  prime_array ||= [] 
  prime = Prime.new
  for num in (1..n)
  return prime_array

Can somebody help me, it fails on the "end"s, when I delta them, I get the error they are expected, when I don’t delete them, it tells me, they are wrong.

Take a look @designninja03516 an @dr_tunk code??