Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Loops Ruby Loops The Ruby Loop

Matthew Mercaldi
Matthew Mercaldi
1,316 Points

why my loop do statement is not working

I am at the part where I need to Add numbers to an array until the number of items in the array are greater than 3. I found the right answer but I am curious why my answer does not work:

loop.rb
numbers = []

number = 0

loop do
  number = number + 1
  numbers.push(number)
  if numbers.count > 3
    break
  end
end

2 Answers

Michael Hess
Michael Hess
24,512 Points

Hi Matthew,

You may want to use numbers.size to check if the array is equal to 3. If the array size is 3 then the break statement inside the if-statement will be executed.

loop do
  number = number + 1
  numbers.push(number)
  if numbers.size == 3
    break
  end
end

If you have any further questions feel free to ask! Hope this helps!

Michael Hess
Michael Hess
24,512 Points

My pleasure -- glad I could help!