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

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.

1 Like

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