There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
can someone break this code down for me and explain. I took hours upon hours and I could not figure it out so I requested for the solution and now that I have it, I can see why I wouldn’t of never understood it.
i = 0
loop do
print “Ruby!”
i += 1
break if i == 30
end
I don’t understand how it loops “Ruby!” and then it is says that 0 (i) is added 1 and assigned back to 0 and then to break if it equals to 30. How do these numbers tell the loop to keep “looping” Ruby! 30 times?
From what I understand,
print “Ruby!”
will happen once every time the loop happens.
*check if looping condition is true (keeps happening until i == 30, starting with i being 0)
*if true, print “Ruby!”
*increase i by 1 (at this point, i is 1)
*this is the end of loop.
*now, back from step 1! check if looping condition is true…
if true, print “Ruby!”
*increase i by 1 (at this point, i is 2)
…and so forth, until at the point that i is 30, by which we would have already printed “Ruby!” 30 times,
the loop will BREAK and stop executing.
print “Ruby!” is inside the loop so once the loop breaks it will no longer print “Ruby!”
Here is a different approach to the question that will give you the same results
(I think you were trying to understand the question like this, which is where you might have been thrown off by the solution but at the end of the day, the result is the same x30 “Ruby!”):
m=0
loop do
m+=1
break if m==30
end
m.times{print “Ruby!”}
Here, I made a loop to get a number m to become 30
and used the .times method to print “Ruby!” instead of printing “Ruby!” within a loop.
I was wondering how i and “Ruby!” are linked. How does it know to print ruby instead of just numbers when I assigned i but said print something else. My first code was like this:
I am stuck here and want to break this laptop. It’s so frustrating for me. I am a former school teacher so this is a whole new language I am trying to understand and it has been difficult at times.