Here is a simple program that stores entered words in an array, then,
once a blank line is entered, displays them sorted alphabetically
(another example from Pine’s book.) Here are two ways of accomplishing
this.
#1:
puts ‘Enter words to sort them alphabetically. When you are finished,
press “return” on an empty line’
words = []
while true
word = gets.chomp
if word == ‘’
break
end
words.push word
end
puts ‘Here they are:’
puts words.sort
–
#2:
puts ‘Use this application to sort any number of words. Enter them
below:’
words = []
while true
word = gets.chomp
words.push word
if word ==’’
break
end
end
puts ‘Here are the words you entered, sorted:’
puts words.sort
–
Which one is more correct? The former was presented by Pine as a
possible solution, and the latter is what I came up with after
consulting this suggestion.