To Be or Not to Be - conditional assignment


I get stuck in exercise “To Be or Not to Be” in this way:

The challenge here, is refactoring the line 14 using conditional operators.

The code before i start refactoring was:
prime_array = [] if prime_array.nil?

To refactor this code using conditional assignment, i can use “||=” to assign a certain value to “prime_array” if this variable are not carrying values defined before the actual assignment.

So, i think that a good way to do this, is replacing:
prime_array = [] if prime_array.nil?
prime_array ||= []


If not, what’s wrong with my code?


This issue has come up before but from our end there is no telling what the real cause is… SCT? Ruby version?

Here is some code that will get you around the issue that uses new syntax (assuming it is a Ruby version problem)

require 'prime' 

def first_n_primes(n)
  "n must be an integer." unless n.is_a? Integer
  "n must be greater than 0." if n <= 0
  Prime.first n

puts first_n_primes(10)

There is a topic (from a month or two ago) which explains the above and how I came about it. Just need to suss it out.


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