Ordering Steps in a Program

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.
--
Posted via http://www.ruby-forum.com/.

The difference is that yours will include the empty string in the
array to be sorted.

···

On Sat, Jul 18, 2009 at 3:23 PM, Max Norman<maxnorman@comcast.net> wrote:

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.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

The difference is that yours will include the empty string in the
array to be sorted.

Yes, I see--because the push method is before the if.

···

--
Posted via http://www.ruby-forum.com/\.