Please explain to me this in detail?


#1



https://www.codecademy.com/pt/courses/ruby-beginner-en-Zjd2y/0/6?curriculum_id=5059f8619189a5000201fbcb


someone please explain this code to me. i understand the beginning part. but this last part ...
...
.....
prime_array ||= []
prime_array ||= nil

prime = Prime.new

n.times{ prime_array << prime.next }
prime_array
end

first_n_primes(10)
...
..... is giving me headache. whats going on here?


def first_n_primes(n)

  return "n must be an integer." unless n.is_a? Integer
  
  return "n must be greater than 0." if n <= 0
  
  prime_array ||= [] 
  prime_array ||= nil
  
  prime = Prime.new
  
  n.times{ prime_array << prime.next }
prime_array
end

first_n_primes(10)

thanks


#2

Ok, let's start from the beginning, step by step.


prime_array ||= [] 
prime_array ||= nil

This part of the code is badly written, it does not make much sense. Let me explain. This strangely looking operator a ||= b can be expanded to more readable form:

a || a = b

So it basically means that if a is a nil or false then assign to a value of b.

Let's get back to our real code, what is the meaning of these two lines? The first line checks if the prime_array is nil or false, and if this is true it changes the value of prime_array to an empty list. That makes sense (sort of). But now we know that value of prime_array is certainly not nil or false, so the second line is simply redundant.

Better solution?

prime_array = [] if prime_array.nil?

It looks better and is definitely more readable. And it's definitely a better way to handle default values of optional parameters, especially when they are boolean. Don't trust codecademy.

Or:

prime_array = []

You always want to calculate list of n first primes, so why not just start with an empty list?


prime = Prime.new

This line creates a new instance of class Prime called prime. Classes will be discussed later.


n.times{ prime_array << prime.next }

n is the number of primes you want to generate, right? So n.times executes n times the attached block. In this block, Ruby generates the next prime number (prime.next) and then adds it (pushes it) to the array called prime_array.

So the overall effect of this line is that you get the array with n first primes.


prime_array

It simply means that this value will be returned as a result of the function.


first_n_primes(10)

This line is a function call. It means that we want our function first_n_primes to generate list of the 10 first prime numbers.


#3

so basically Prime.new is an object(Class) on its own like Proc.new or Hash.new right ? so can i make my own object like Fun.new or this objects are standardized ??
a ||= b isnt it a = a || b which both sides are true or false but one must be true for this to be true?
when you say the first line you actually mean the 2nd line right ? if so when you say the 2nd part is redundant you mean ...=nil is redunadant ? ..=[] is enough ?

Anyway it makes sense. i should revisit the whole topic i guess Thanks.


#4

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