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

Patrick Shushereba
Patrick Shushereba
10,911 Points

Ruby Loops

I'm not sure what's wrong with my code. I tried to add the value of number to the array, and I incremented the number variable. I tried to set up an "if" statement so that when the number is greater than 3 the loop breaks. Can someone give me some insight?

loop.rb
numbers = []

number = 0
loop do
  numbers.push[number]
  number += 1
  if number > 3
    break
  end
end
# write your loop here

1 Answer

Sage Elliott
Sage Elliott
30,003 Points

Hello, Patrick! You're very close but there are just few things you need to change.

  1. When pushing you'll need to surround the "number" with parentheses not brackets.

  2. The "s" is missing when trying to evaluate the array. Instead you're looking at the number variable.

  3. Call the length method on the array to receive the amount of items

  4. lastly array indexes start at 0 so you actually would use 2 for the comparison in the if statement. 0 = 1, 1 = 2, and 2, = 3.

You can look below for reference, I hope that helps you!

numbers = []

number = 0

# write your loop here
loop do
  numbers.push(number)
  number += 1
  if numbers.length > 2
    break
  end
end