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

Philip Vaarskov
Philip Vaarskov
2,252 Points

Using the loop construct, add the current value of number to the numbers array. Inside of the loop, add 1 to the number

What am i doing wrong?

loop.rb
numbers = []

number = 0

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

 Bummer! NoMethodError: undefined method `push' for 0:Fixnum Did you mean? puts

2 Answers

Hi Philip,

You want to push onto the numbers array; pass in number as the method argument. Your error is saying that number doesn't have a push method - that's because it isn't an array; numbers is! :smile:

numbers.push(number)

Also, you want to do that first in the loop, before you increment number otherwise your array will never have the value zero in it. The question wants add the current value of number to the numbers array - this includes the initial zero value. Next up; to increment, use += rather than =+.

That all looks like:

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

Let me know how you get on.

Steve.

Philip Vaarskov
Philip Vaarskov
2,252 Points

Thank you so much, it worked :)