So I am reading ’ Learn to Program’ by Chris P… One of the exercises
there is to build and sort an array.
This is the question: Write the program we talked about at the beginning
of this chapter, one that asks us to type as many words as we want (one
word per line, continuing until we just press Enter on an empty line)
and then repeats the words back to us in alphabetical order. Make sure
to test your program thoroughly; for example, does hitting Enter on an
empty line always exit your program? Even on the first line? And the
second? Hint: There’s a lovely array method that will give you a sorted
version of an array: sort. Use it!
And this is my attempt
random_words = []
input = gets.chomp
while input!=’’
puts 'Type any word you want: ’
input = gets.chomp
random_words.push input
end
puts random_things.sort
So it does give me a list when I hit ENTER on empty line. The only
problem is that the list never includes the first word I type. (i.e. if
I typed ‘Flower’ on the first line, ‘Flower’ never shows up on the
sorted list… can anybody explain that to me ?? Thanks
— Beginner beginner on Ruby.