Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Loops Ruby Loops The Ruby Loop

Ruby Loops Confusion

So, I realize that my code is pretty far off, but I'm confused here.

1) I can't seem to add the value of 'number' to the numbers array without erasing the array and setting the value to the '0'.

2) I'm pretty confused about what the question is asking me to do--and I think my code reflects this!

Any help would be greatly appreciated!

loop.rb
numbers = []

number = 0

numbers = number
loop do 
  number + 1
  if numbers > 3
    break
  end

end

1 Answer

Tim Knight
Tim Knight
28,888 Points

Hi Cody,

I haven't gone through this particular course so I'm not sure what concepts they've taught that led up to this, but I think I can give you enough information to help you solve your issue.

First you're wanting to append the value to the array with each iteration of the loop so instead of numbers = number on the outside of the loop you'll want to put numbers << number inside of the loop. The << method is a shorthand way of appending an item to a list in Ruby (shorthand of the push method). Next you want to take the current value of number (which is 0) and add one to it. You would do this with += so you're saying "add this value to the current value and make the variable equal that". Since it wants you to end the loop at 3 items you'll want to do >= instead of just > so it'll end right at 3 instead of just items greater than.

With all of that you should have something that looks like this:

numbers = []
number = 0

loop do
  numbers << number
  number += 1
  break if numbers.size >= 3
end