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

Breaking a loop if it has more than 3 items

In this challenge, we create a loop that adds 1 to the variable "number", which starts at 0. Then if the array "numbers" has more than 3 items, we are to break the loop. My code is below, I'm stumbling on the breaking the loop part--the error message I keep getting is "no conversion of fixnum into array". Any help is appreciated. Thanks!

loop.rb
numbers = []

number = 0

# write your loop here
loop do
  numbers + number
  number + 1
  if numbers > [0, 1, 2]
    break
  end
end

2 Answers

L B
L B
28,323 Points
numbers = []

number = 0

# write your loop here
loop do
  #numbers + number Here you are trying to do: Array add an integer which is not compatible
  numbers.push number #Instead use the push method which is part of the array library
  number + 1
 # if numbers > [0, 1, 2] 
  if numbers.length >= 3 #Use the length method from the arrays library which returns the size of an array
    break
  end
end

Thank you very much!

Chris Adamson
Chris Adamson
132,143 Points

For this challenge, your incrementing a number by 1 and adding it to an array. To see if the array length is long enough you use the length method:

numbers = []

number = 0

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

end