My while loop won't end!

Hi everyone. I’m working on an assignment for class, and I can’t see
where my mistake is. I am asking the user for input (words), and while
the user input != " " it should ask for another word. Once the user
simply pushes “enter” without typing anything, it should quit, and show
the array order alphabetically. However, when I push enter without
typing anything, it asks me for another word, rather than ending.

I would really appreciate some help on this, as it is important for me
to understand WHY it isn’t working!

Here is my code so far:

words = []

puts “Please enter as many words as you would like, one line at a time.”
puts “To quit the program, simply push enter without typing any words.”

user_words = gets.chomp

while user_words != " " do
words.push user_words
puts “next word please”
user_words = gets.chomp

end

puts “Here are the words you entered sorted alphabetically:”

puts words.sort

Oh. My. Gosh. I cannot believe that I missed that!! My brain is so
tired! Haha. Thank you SO MUCH for your help! That fixed it! You are
my hero!

Thank you!!

while user_words != " " do

The loop won’t end when you hit enter because you’re telling it to only
end the loop if you hit SPACE and then ENTER.

Do this instead

while user_words != “” do

end

Jake D. wrote in post #1161417:

Oh. My. Gosh. I cannot believe that I missed that!
Thank you!!

I know how it feels xDD
You’re welcome.