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

Andrew Insley
Andrew Insley
7,014 Points

I though I got the loop principle, but this question has completely stumped me. Would really appreciate some help???????

I get the loop principles, the thing I am having trouble with is adding the values into the array.

Could really do with a hand to stop me going mad.

Thank you

loop.rb
numbers = []

number = 0

numbers[0] = number 

1 Answer

Seth Reece
Seth Reece
32,867 Points

Hi Andrew,

Adding items to an array can be done with push. e.g. You could also use unshift to add to the beginning, or insert to add in a certain position, but I think push is fine for this task.

numbers = []

number = 0

loop do
  numbers.push(number)
  number += 1
    if numbers.count >= 3
      break
    end
end
Andrew Insley
Andrew Insley
7,014 Points

I would like to say I was close, but I wasn't. Thanks