I'd rather do this because the test is much easier:

words = []

while ( line = gets )
  words << line.chomp
end

However, there is a much shorter solution available:

puts ARGF.readlines.sort

Or even using "sort" directly (on a Unix like system):

$ sort <<XXX
> foo
> bar
> baz
> XXX
bar
baz
foo

Kind regards

robert