On Aug 17, 2:04 pm, William James <w_a_x_... / yahoo.com> wrote:
> The original poster wants the loop to end when the user presses
> <Enter>
> without entering any text.  How about this?
>
> words = []
> while line = gets  and  line.chomp! != ""
>   words << line
> end

Might as well simply evaluate the assignment expression directly:

words = []
while (line = gets.chomp) != ''
  words << line.chomp
end
puts words.sort

While that fulfills the original spec, I agree with Dan's comment
regarding <ctrl>-D or EOF. It would be nice to handle that to allow
piping a file of words to the program. Before I noticed Robert's post,
I came up with the following:

puts ARGF.inject([]) {|memo,obj| memo << obj}.sort

although fun, it's way too verbose. I ended up shortening Robert's to
this:

puts ARGF.sort

If you don't want to create a .rb file, just:

ruby -e 'puts ARGF.sort'

:)