cool_words = []
while true
cool_words.push gets
break if gets.chomp == ''
end
puts cool_words
It is only pushing the first entry then the third and then the fifth. I think it is the way I have it breaking out of the loop because without the break method it doesn’t happen.
I need it to break out of the loop when I hit enter on an empty line.
Thanks in advance!
You are calling
getstwice in the loop. The first time it is being pushed into the array. The second time it is comparing against an empty string for loop breaking. But each time it is asking for a new line.You only want to call
getsone time per loop. So you can save it in a variable, and then use that variable multiple times later in the code.UPDATE: @MicahelKohl in the comments points out that you can accomplish the above task more elegantly like this: