FAQ: Loops & Iterators - The Loop Method

This community-built FAQ covers the “The Loop Method” exercise from the lesson “Loops & Iterators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise The Loop Method

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi, I was just exploring the loop function with gets.chomp syntax, but it seems like the gets.chomp input doesn’t work in the loop function. Is there a reason for this?

This is how my code looks like:

print "Tell me your age."
age = Integer(gets.chomp)

loop do
  age += 1
  print "#{age}"
  break if age <= 30
end

Instead of looping up to 30, it only looped up to 26. Why is it so? Is there another way to make this work?

1 Like

Hello :slight_smile:

I think that the problem is caused by this condition.

Let’s say that I am 26 years old, age = 26. Now we get into the loop block, age += 1 results in age = 26 + 1 = 27. In the next line of the code 27 is printed out to the console. And now if 26 <= 30 (which is true) we break and the loop stops.

And if my input is 55 then we have an infinite loop.


The condition should be probably changed to age >= 30.

2 Likes

9 posts were split to a new topic: “rb” before a line of example code

Hello, I was just wondering why the example code block uses print "#{i}" when print i seems like it would do the same thing

2 Likes

SOLVED
When completing the loop method, my “loop” and “break” is entering orange like a variable:
i = 20
loop do
i -= 1
print “#{i}”
break if i <= 0
end

and the console is printing:
191817161514131211109876543210
which i would have expected to print vertically.

why is this happening, the task is registering as complete and correct, it just doesn’t correlate with the rest of the lessons.

Thanks

I wouldn’t pay much attention to the colors you find on Codecademy. These were picked by the developers, but outside of this environment, colors could be completely different.

I understand that it can be puzzling, sometimes. Especially when the lesson’s example gives you loop do in blue, whilst showing up in orange in the editor… Just remember the above comment.


print "#{i}"

wouldn’t print the result vertically. For it to appear that way, you’ll want to use a new line "\n".

So, you could do this

print "#{i}\n"

or, you could simply use puts, instead of print.

The primary difference between puts and print is that puts adds a newline after executing, and print does not.

3 Likes

Okay, that’s great info, thank you.

1 Like

It will print vertically if you use puts instead of print

1 Like

Is there a reason print "#{i}" was used in the Loop method code instead of print i or puts i like in the ‘for’ and ‘while’ loops?

Hi everyone.

I don’t understand why we need to use string interpolation on i (screenshot attached).

Could anyone please explain? Thanks

While i and #{i} produce the same output in this exercise, I think it’s just a chance to reinforce the topic of string interpolation.

1 Like