Pine's tutorial "Word Sorter" exercise

Solution for the 1st exercise in chapter 8:

(I'm skipping the 2nd exercise - just a bunch of busywork)

-----------------------------------------------------------------------
input =

while input.last != ''
  input.push gets.chomp
end

puts input.sort
-----------------------------------------------------------------------

···

On Apr 1 2006, 8:12 pm, Jan_K <n...@none.com> wrote:
----------
To reset slightly, this is an exercise that tells you to accept words
from the user until they just hit Enter, then alphabetize the list.

My problem with the exercise is that the sort method alphabetizes
capital letters before lowercase letters. Therefore, if input ==
[ant, bear, Zebra], the output of

puts input.sort

is

Zebra
ant
bear

...which is clearly not what most users will expect. Short of
changing the line in the program to read

  input.push gets.chomp.downcase

(thus altering the entered words in a possibly undesirable way), how
can we get the sort to work the way we might expect? Thanks in
advance.

Randy...

#sort and #sort_by accept blocks that tell how to do the comparison, so:

puts input.sort_by{|a| a.downcase}

is the simplest solution.

PLUG: The RubyMentor project is looking for newbies to help!
http://rubymentor.rubyforge.org/wiki/wiki.pl

···

On 2/19/07, Randy Shipp <randyshipp@gmail.com> wrote:

On Apr 1 2006, 8:12 pm, Jan_K <n...@none.com> wrote:
> Solution for the 1st exercise in chapter 8:
>
> (I'm skipping the 2nd exercise - just a bunch of busywork)
>
> -----------------------------------------------------------------------
> input =
>
> while input.last != ''
> input.push gets.chomp
> end
>
> puts input.sort
> -----------------------------------------------------------------------

----------
To reset slightly, this is an exercise that tells you to accept words
from the user until they just hit Enter, then alphabetize the list.

My problem with the exercise is that the sort method alphabetizes
capital letters before lowercase letters. Therefore, if input ==
[ant, bear, Zebra], the output of

puts input.sort

is

Zebra
ant
bear

...which is clearly not what most users will expect. Short of
changing the line in the program to read

  input.push gets.chomp.downcase

(thus altering the entered words in a possibly undesirable way), how
can we get the sort to work the way we might expect? Thanks in
advance.

Randy...