2 To Be or Not to Be


#1

https://www.codecademy.com/courses/learn-ruby/lessons/the-refactor-factory/exercises/to-be-or-not-to-be?action=lesson_resume&link_content_target=interstitial_lesson

<In what way does your code behave incorrectly? Include ALL error messages.>
I put conditionnal assignement operator instead of if statment . However it does not work . Why ?
This exercice seemed to me to be easy ???

No ERROR MESSAGE !
Thanks for your help ! The Codecademy Team members!

prime_array||= [] 


#2

There seems to be a conflict in the version of Ruby expected, and the one present. In newer versions, .new is gone, and so is .next as I understand it. Consequently the currently prescirbed code does not execute properly. Without some confirmation from the dev team, it’s impossible to say what the issue truly is–Version or SCT?

Prime.new can now be written as Prime.instance and the array no longer needs to be declared since the above method returns an array.

What’s interesting is that we can run the same methods on the Class as on the instance so it is not even necessary.

Anyway, it will take some follow-up reading to get to the bottom of the changes in Ruby, and I will need to find out just what the issue is with this lesson. I do know that the newer version syntax does work so have a strong suspicion the issue here is version related.

Following is code that will work. It is stripped right down to only the essential code needed to complete the assignment. I’ll leave it with you to uncover the logic behind it.

require 'prime'

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

puts first_n_primes(10)

Sorry I couldn’t be more help. Dig around this category for the topic where I explained how this code came to be. It took me an afternoon to figure it out.


#3

Thanks a lot however mtf !


#4

One additional suggestion that will let you work through the entire exercise:

From the documentation “Prime.new is now obsolete. Use Prime.instance.each or simply Prime.each.”

I changed the following line:

prime = Prime.new

to:

prime = Prime.each

and all of the steps in the exercise now work.

http://ruby-doc.org/stdlib-1.9.3/libdoc/prime/rdoc/Prime.html#method-i-each-label-Note


#7

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